1

I am trying to wrap me head around cross compiling C++ program for Android.

It is a simple console program.

My Cmake Configuration is like so:

cmake_minimum_required(VERSION 3.4.1)
project(app)
set(NDK "/Users/user/Desktop/NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin")
set(CMAKE_CXX_FLAGS "")
set(CMAKE_C_COMPILER "${NDK}/clang")
set(CMAKE_CXX_COMPILER "${NDK}/armv7a-linux-androideabi16-clang++")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../Output")
file(GLOB Source "Source/*.cpp")
add_executable(${PROJECT_NAME} ${Source})

compiling succeeds eventually I get linking error like so:

ld: error: unknown argument '-search_paths_first'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [../Output/app] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2

When I change armv7a-linux-androideabi16-clang++ compiler to clang++, i get errors like this:

ld: error: unknown argument '-dynamic', did you mean '-Bdynamic'
ld: error: unknown argument '-arch'
ld: error: unknown argument '-platform_version'
ld: error: unknown argument '-syslibroot'
ld: error: unknown argument '-search_paths_first'
ld: error: unable to find library -lto_library
ld: error: cannot open /Users/user/Desktop/NDK/toolchains/llvm/prebuilt/darwin-x86_64/lib/libLTO.dylib: No such file or directory
ld: error: cannot open x86_64: No such file or directory
ld: error: cannot open macos: No such file or directory
ld: error: cannot open 11.0.0: No such file or directory
ld: error: cannot open 11.3: No such file or directory
ld: error: cannot open /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk: Is a directory
ld: error: CMakeFiles/app.dir/Source/Source.cpp.o: unknown file type
ld: error: unable to find library -lc++
ld: error: unable to find library -lSystem
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [../Output/app] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2

None of the binaries are in my search path, could this be the problem?

D.elp
  • 147
  • 5
  • It is wrong to set a compiler after the `project` call. See e.g. that [my answer](https://stackoverflow.com/a/63944545/3440745). For cross-compiling you usually need to create a [toolchain](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html). – Tsyvarev Oct 24 '21 at 15:29
  • Thank you, your answer on that treat partially helped me and up voted it. – D.elp Oct 25 '21 at 22:30

1 Answers1

0

If you don't want to use a toolchain file as discussed in the comments above, one option for this case is to trick cmake into avoiding these macOS specific flags. From my testing you can use -DCMAKE_SYSTEM_NAME=Linux and -DANDROID=ON to do this

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110