如何为Android编译libgit2?编译报错求助
编译libgit2 for Android时的NDK编译问题
背景
开发一款基于libgit2的Flutter应用,Linux环境下编译libgit2无问题。现在需要添加Android支持,使用Android NDK(r26及以下),配置的toolchain.cmake如下:
set(CMAKE_SYSTEM_NAME Android) set(CMAKE_SYSTEM_VERSION 34) set(CMAKE_ANDROID_ARCH_ABI arm64-v8a) set(CMAKE_ANDROID_NDK /opt/android-ndk) set(OPENSSL_INCLUDE_DIR "/实际路径/openssl/include") set(OPENSSL_CRYPTO_LIBRARY "/实际路径/openssl/libcrypto.so") set(OPENSSL_SSL_LIBRARY "/实际路径/openssl/libssl.so")
注:配置中已使用OpenSSL仓库的实际路径,而非示例中的/path/to/openssl;用NDK编译OpenSSL无问题。
问题1:NDK r26下CMake无法通过编译器测试
使用上述配置编译libgit2时,CMake报错:
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" is not able to compile a simple test program.
发现/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang是指向clang-17的文本文件,尝试在toolchain中手动指定编译器路径但无效:
set(CMAKE_C_COMPILER /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17) set(CMAKE_CXX_COMPILER /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17)
将该文本文件替换为软链接后,出现新错误:
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" is not able to compile a simple test program. It fails with the following output: Change Dir: '/home/jakub/Projekty/dpc/lib/libgit/build/arm64/CMakeFiles/CMakeScratch/TryCompile-03LVGf' Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_f226c/fast /usr/bin/gmake -f CMakeFiles/cmTC_f226c.dir/build.make CMakeFiles/cmTC_f226c.dir/build gmake[1]: Vstupuje se do adresáře „/media/data/Všechny Projekty/dpc/lib/libgit/build/arm64/CMakeFiles/CMakeScratch/TryCompile-03LVGf“ Building C object CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=aarch64-none-linux-android34 -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -O2 -g -DNDEBUG -MD -MT CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o -c /home/jakub/Projekty/dpc/lib/libgit/build/arm64/CMakeFiles/CMakeScratch/TryCompile-03LVGf/testCCompiler.c Linking C executable cmTC_f226c /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f226c.dir/link.txt --verbose=1 /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=aarch64-none-linux-android34 -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -O2 -g -DNDEBUG -Wl,--build-id=sha1 -Wl,--no-undefined-version -Wl,--fatal-warnings -Wl,--gc-sections -Qunused-arguments -Wl,--no-undefined -Wl,--gc-sections CMakeFiles/cmTC_f226c.dir/testCCompiler.c.o -o cmTC_f226c -latomic -lm clang: error: unable to execute command: posix_spawn failed: Exec format error clang: error: linker command failed with exit code 1 (use -v to see invocation)
执行/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --version输出:
Android (10087095, +pgo, +bolt, +lto, -mlgo, based on r487747c) clang version 17.0.2 (https://android.googlesource.com/toolchain/llvm-project d9f89f4d16663d5012e5c09495f3b30ece3d2362) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin
问题2:NDK r22b下出现头文件编译错误
尝试使用NDK r22b(或更早版本)时,配置可完成且接近编译完成,但出现头文件错误,无法识别inline关键字:
/opt/android-ndk-r22/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/linux/swab.h:40:8: error: unknown type name 'inline' static inline __attribute__((__const__)) __u32 __fswahw32(__u32 val) { ^ /opt/android-ndk-r22/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/linux/swab.h:40:47: error: expected ';' after top level declarator static inline __attribute__((__const__)) __u32 __fswahw32(__u32 val) {
寻求相关技术解决方案。
内容的提问来源于stack exchange,提问作者Jakub Slabý
相关产品推荐
相关产品推荐

