You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Ubuntu 16.04 Linux上交叉编译Android的C++命令行应用及Qt支持?

Cross-Compiling Qt-Based C++ Command-Line Apps for Android (Termux Execution)

Alright, let's break this down step by step— I've set up this exact workflow before on Ubuntu 16.04, so I know what works. We'll skip QtCreator entirely and stick to command-line tools, from installing dependencies to running the app in Termux.

1. Install Required Packages on Ubuntu 16.04

First, update your system and install the base tools we'll need:

sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential cmake wget unzip

2. Set Up Android NDK (Required for Cross-Compilation)

Android apps need the NDK for cross-compiling toolchains. We'll use NDK r21e (it's compatible with Ubuntu 16.04's older GCC and works well with Termux):

  • Go to the Android NDK archive page and download the r21e Linux x86_64 package.
  • Unzip the downloaded file in your working directory:
    unzip android-ndk-r21e-linux-x86_64.zip
    
  • Set an environment variable pointing to the unzipped NDK folder (add this to ~/.bashrc if you want it persistent across sessions):
    export ANDROID_NDK=$(pwd)/android-ndk-r21e
    

3. Install Qt for Android (Command-Line Only)

We need a Qt build targeted at Android arm64-v8a (Termux's most common architecture). We'll use Qt 5.12.10— it's the last Qt version that supports Ubuntu 16.04 natively:

  • Go to the Qt archive page, navigate to Qt 5.12 > 5.12.10, and download the Linux x64 installer.
  • Make the installer executable:
    chmod +x qt-opensource-linux-x64-5.12.10.run
    
  • Run the installer in command-line mode. When prompted, make sure to select the Android ARM64-v8a component under Qt 5.12.10 (you can skip desktop components if you don't need them):
    ./qt-opensource-linux-x64-5.12.10.run
    

Once installed, note the path to your Qt Android installation— it'll look something like ~/Qt5.12.10/5.12.10/android_arm64_v8a.

4. Write a Test Qt C++ Program

Create a simple command-line app to test. Make a file named main.cpp:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    qDebug() << "Hello Qt from Android Termux!";
    return app.exec();
}

5. Configure CMake for Cross-Compilation

Create a CMakeLists.txt file in the same directory as main.cpp:

cmake_minimum_required(VERSION 3.10)
project(QtAndroidTermuxTest)

set(CMAKE_CXX_STANDARD 11)

# Point to your Qt Android installation (update the path to match yours)
set(Qt5_DIR ~/Qt5.12.10/5.12.10/android_arm64_v8a/lib/cmake/Qt5)
find_package(Qt5 REQUIRED COMPONENTS Core)

# Build our executable
add_executable(QtAndroidTermuxTest main.cpp)
target_link_libraries(QtAndroidTermuxTest Qt5::Core)

# Android cross-compilation settings
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 21)  # Minimum Android version (5.0, supported by Termux)
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(CMAKE_ANDROID_NDK ${ANDROID_NDK})
set(CMAKE_ANDROID_STL_TYPE c++_shared)  # Use shared STL (matches Termux's environment)

6. Compile the App

Now let's build the binary:

# Create a build directory to keep things clean
mkdir build && cd build
# Run CMake to configure the cross-compilation environment
cmake .. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
# Compile the app using all available CPU cores
make -j$(nproc)

After this, you'll have an executable named QtAndroidTermuxTest in the build directory.

7. Run the App in Termux

First, prepare Termux to run the app:

  • Open Termux and install the required Qt library:
    pkg update && pkg install -y qt5-qtcore
    

Then transfer the binary to Termux:

  • If your Android device is connected via USB debugging, use adb:
    adb push build/QtAndroidTermuxTest /data/data/com.termux/files/home/
    
  • Alternatively, use scp if you have SSH set up in Termux.

Finally, run the app in Termux:

chmod +x QtAndroidTermuxTest
./QtAndroidTermuxTest

You should see the output: Hello Qt from Android Termux!

Key Notes to Avoid Issues

  • NDK Version: Stick to r21e— newer NDKs require a newer GCC than Ubuntu 16.04 provides.
  • Qt Version: Qt 5.12.x is the last compatible with Ubuntu 16.04. Newer Qt versions will throw dependency errors.
  • Shared STL: We use c++_shared because Termux has the shared libc++ library installed. Static linking can cause runtime crashes.
  • Termux Libraries: Always install the matching Qt package in Termux (e.g., qt5-qtcore for our test app) to avoid missing library errors.

内容的提问来源于stack exchange,提问作者x__x

火山引擎 最新活动