Jump to content

Qt for HarmonyOS/qt5 Host Build

From Qt Wiki

Native Compilation of Qt 5.12.12 on HarmonyOS PC (aarch64)

This article describes the complete workflow for setting up the build environment using the Harmonybrew package manager on a HarmonyOS PC (aarch64 architecture, Hongmeng kernel), natively compiling Qt 5.12.12 (the `tqtc/harmonyos-5.12.12` branch) from source, and packaging it into a HarmonyOS application.

1. Install Harmonybrew Package Manager

Harmonybrew is the Homebrew package manager for the HarmonyOS platform. Run the following command in your terminal to install it:

zsh -c "$(curl -fsSL https://harmonybrew.atomgit.com/install.sh)"

Configure environment variables and append them to `~/.zshrc`:

echo 'eval "$(/storage/Users/currentUser/.harmonybrew/bin/brew shellenv)"' >> ~/.zshrc
eval "$(/storage/Users/currentUser/.harmonybrew/bin/brew shellenv)"

2. Install Build Dependencies

Use Harmonybrew to install the toolchain and OpenHarmony SDK required for building Qt:

brew install make              # GNU Make 4.4.1
brew install perl              # Perl (Qt build system dependency)
brew install python            # Python 3 (qtdeclarative module dependency)
brew install ninja             # Ninja build tool
brew install gperf             # gperf (Qt build dependency)
brew install ohos-sdk          # OpenHarmony SDK (includes sysroot + llvm toolchain + hdc)

3. Configure Environment Variables

Persist the OHOS SDK environment variables by writing them to `~/.zshrc`:

cat >> ~/.zshrc << 'EOF'

# OHOS SDK environment variables (for Qt compilation)
export NATIVE_OHOS_SDK="/storage/Users/currentUser/.harmonybrew/opt/ohos-sdk/native"

# Add SDK's llvm/bin and Python to PATH
export PATH="$NATIVE_OHOS_SDK/llvm/bin:/storage/Users/currentUser/.harmonybrew/opt/python@3.14/libexec/bin:/storage/Users/currentUser/.harmonybrew/bin:$PATH"
EOF

source ~/.zshrc

4. Obtain Qt Source Code

Reference: How to obtain Qt for HarmonyOS source code

5. Configure Qt (Out-of-source Build)

Create an independent build directory outside the source tree and run `configure`:

BUILD_DIR=/storage/Users/currentUser/workspace/Qt-5.12.12/qt5.12.12-build
INSTALL_DIR=/storage/Users/currentUser/Qt-5.12.12-harmonyos
SRC_DIR=/storage/Users/currentUser/Qt-5.12.12/tqtc-qt5

mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

"$SRC_DIR/configure" \
    -platform ohos-native-clang \
    -opensource \
    -confirm-license \
    -release \
    -nomake tests \
    -nomake examples \
    -skip qtwebengine \
    -skip qtactiveqt \
    -skip qtandroidextras \
    -skip qtcanvas3d \
    -skip qtfeedback \
    -skip qtgamepad \
    -skip qtmacextras \
    -skip qtpim \
    -skip qtpurchasing \
    -skip qtremoteobjects \
    -skip qtsystems \
    -skip qtwayland \
    -skip qtwinextras \
    -skip qtx11extras \
    -skip qtvirtualkeyboard \
    -prefix "$INSTALL_DIR" \
    QMAKE_CXXFLAGS+="-Wno-error" \
    QMAKE_CFLAGS+="-Wno-error"

Key Parameter Descriptions:

  • `-platform ohos-native-clang`: Uses the OHOS native compilation mkspec (for native compilation on arm64 devices).
  • `QMAKE_CXXFLAGS+="-Wno-error"` / `QMAKE_CFLAGS+="-Wno-error"`: Disables `-Werror` to prevent the build from failing due to strict warnings from newer Clang compiler versions.

6. Compilation and Installation

Perform a parallel build utilizing all CPU cores:

cd "$BUILD_DIR"
make -j$(nproc)

Note: During multi-core parallel compilation, if individual modules fail due to race conditions, simply rerun `make -j$(nproc)` to resume. The build is successful when the output consists entirely of "Nothing to be done".

Run the installation after compilation finishes:

make install

7. Verify the Installation

1. Check the `qmake` version:

"$INSTALL_DIR/bin/qmake" --version

Expected output:

QMake version 3.1
Using Qt version 5.12.12 in /storage/Users/currentUser/Qt-5.12.12-harmonyos/lib

2. Check core libraries and the QPA platform plugin:

ls "$INSTALL_DIR/lib/libQt5Core.so" \
   "$INSTALL_DIR/lib/libQt5Gui.so" \
   "$INSTALL_DIR/lib/libQt5Widgets.so" \
   "$INSTALL_DIR/lib/libQt5Qml.so" \
   "$INSTALL_DIR/lib/libQt5Quick.so"

# Check OHOS QPA platform plugin
ls "$INSTALL_DIR/plugins/platforms/libqohos.so"

Appendix: Packaging and Building HarmonyOS Applications

Write Project Files

Create a simple Qt/QML application (e.g., in a directory named `myapp`):

`myapp.pro`:

QT += qml quick gui
CONFIG += c++11 qt qml
TEMPLATE = lib
CONFIG -= app_bundle
TARGET = myapp
SOURCES += main.cpp
RESOURCES += qml.qrc

Compile the Application Library

Set environment variables in the terminal and invoke the compiled `qmake` to build the shared library:

export NATIVE_OHOS_SDK="/storage/Users/currentUser/.harmonybrew/opt/ohos-sdk/native"
export PATH="$NATIVE_OHOS_SDK/llvm/bin:/storage/Users/currentUser/.harmonybrew/opt/python@3.14/libexec/bin:/storage/Users/currentUser/.harmonybrew/bin:$PATH"

/storage/Users/currentUser/Qt-5.12.12-harmonyos/bin/qmake myapp.pro
make -j$(nproc)
# Build artifact: libmyapp.so (ELF 64-bit arm64, target aarch64-linux-ohos)

Deployment and Packaging

1. Use `openharmonydeployqt` to deploy templates:

/storage/Users/currentUser/Qt-5.12.12-harmonyos/bin/openharmonydeployqt \
    --template-dir /storage/Users/currentUser/tqtc-qt5/qtbase/src/harmonyos/templates \
    --output-dir /path/to/deploy \
    --library /path/to/libmyapp.so \
    --sdk-dir /storage/Users/currentUser/.harmonybrew/opt/ohos-sdk

2. Copy dependent libraries to the project directory:

# Copy Qt core dependent libraries and plugins
cp /storage/Users/currentUser/Qt-5.12.12-harmonyos/lib/libQt5{Core,Gui,Qml,Quick,Network}.so deploy/entry/libs/arm64-v8a/
cp /storage/Users/currentUser/Qt-5.12.12-harmonyos/plugins/platforms/libqohos.so deploy/entry/libs/arm64-v8a/

# Copy C++ runtime library
find /storage/Users/currentUser/.harmonybrew/opt/ohos-sdk/native -name "libc++_shared.so" -exec cp {} deploy/entry/libs/arm64-v8a/ \;

3. Modify the application entry point to load the library: Edit `deploy/entry/src/main/ets/common/QtAppConstants.ets` and change `APP_LIBRARY_NAME` to the name of the generated dynamic library:

export const APP_LIBRARY_NAME = 'libmyapp.so';

4. Open the `deploy/` directory using DevEco Studio to directly build, sign, and deploy to your device.