Cross compiling a static library for android
Answered
Hello,
I use CLion on mac and I have a c++ project that I want to cross compile for android. Can someone help me with how to create the toolchain and configure the cmake profile profile to achieve this purpose. Below is the toolchain I created
And below is the cmake profile I created
CMalkeLists.txt
cmake_minimum_required(VERSION 3.19)
project(example_cpp_v002)
set(CMAKE_CXX_STANDARD 20)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall --std=c++20 -O3 -fPIC" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -fPIC" )
set(SOURCE_FILES
key_manager/key_manager.cpp
key_manager/key_manager.h
key_manager/char_lists.hpp
key_manager/crypto.hpp
)
add_library(example STATIC ${SOURCE_FILES})
But when I run it I get the following error
"/Users/dziedzorm/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/232.9559.58/CLion.app/Contents/bin/cmake/mac/bin/cmake" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang -DCMAKE_CXX_COMPILER=/Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang++ -G "CodeBlocks - Unix Makefiles" -S /Users/dziedzorm/Documents/Dev/SnooCODE/cpp/snoocode_cpp_v002 -B /Users/dziedzorm/Documents/Dev/SnooCODE/cpp/snoocode_cpp_v002/cmake-build-release-arm
-- The C compiler identification is Clang 17.0.2
-- The CXX compiler identification is Clang 17.0.2
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang
-- Check for working C compiler: /Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang - broken
CMake Error at /Users/dziedzorm/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/232.9559.58/CLion.app/Contents/bin/cmake/mac/share/cmake-3.26/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
"/Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /Users/dziedzorm/Documents/Dev/SnooCODE/cpp/snoocode_cpp_v002/cmake-build-release-arm/CMakeFiles/CMakeScratch/TryCompile-PjpyTM
Run Build Command(s):/Users/dziedzorm/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/232.9559.58/CLion.app/Contents/bin/cmake/mac/bin/cmake -E env VERBOSE=1 /usr/bin/make -f Makefile cmTC_fccd5/fast && /Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/cmTC_fccd5.dir/build.make CMakeFiles/cmTC_fccd5.dir/build
Building C object CMakeFiles/cmTC_fccd5.dir/testCCompiler.c.o
/Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -fcolor-diagnostics -MD -MT CMakeFiles/cmTC_fccd5.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_fccd5.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_fccd5.dir/testCCompiler.c.o -c /Users/dziedzorm/Documents/Dev/SnooCODE/cpp/snoocode_cpp_v002/cmake-build-release-arm/CMakeFiles/CMakeScratch/TryCompile-PjpyTM/testCCompiler.c
Linking C executable cmTC_fccd5
"/Users/dziedzorm/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/232.9559.58/CLion.app/Contents/bin/cmake/mac/bin/cmake" -E cmake_link_script CMakeFiles/cmTC_fccd5.dir/link.txt --verbose=1
/Users/dziedzorm/Library/Android/sdk/ndk/26.0.10636728/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi34-clang -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/cmTC_fccd5.dir/testCCompiler.c.o -o cmTC_fccd5
ld.lld: error: unknown argument '-search_paths_first'
clang-17: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [cmTC_fccd5] Error 1
make: *** [cmTC_fccd5/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
[Failed to reload]
Please sign in to leave a comment.
Is it possible you are missing some CMAKE variables, like the ones below?
I am cross-compiling static libraries for Android on a Mac, and I use this entry in my CMakePresets.json:
{
"name": "android-arm64-v8a - debug",
"generator": "Ninja",
"binaryDir": "<put your build dir here>",
"cacheVariables": {
"CMAKE_SYSTEM_NAME": "Android",
"CMAKE_SYSTEM_VERSION": "28",
"CMAKE_ANDROID_ARCH_ABI": "arm64-v8a",
"CMAKE_ANDROID_NDK": "/Users/Oliver/Library/Android/sdk/ndk/26.0.10636728",
"CMAKE_ANDROID_STL_TYPE": "c++_static",
"CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION": "clang",
"CMAKE_INSTALL_PREFIX": ".<put your install prefix here>",
"CMAKE_BUILD_TYPE": “Debug”
}
Hello!
According to https://stackoverflow.com/a/75034696, setting
CMAKE_SYSTEM_NAME
might be important, as also suggested by oviano.