0

Here is a simplified CMakeLists.txt that I am using to cross-compile a shared library for Android.

cmake_minimum_required(VERSION 3.10)
project(MyTest)
link_directories($ENV{ANDROID_PREBUILT}/gcc/linux-x86/arm/arm-linux-androideabi-4.9/lib/gcc/arm-linux-androideabi/4.9)
add_library(MyTest SHARED MyTest.cpp)

When building, the error I get is:

xxx/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-ld:   
error: cannot find -lgcc

I see that the link directory I specified does have libgcc.a file.

Running make in verbose mode shows the following link command:

xxx/clang/host/linux-x86/clang-2690385/bin/clang++
  --target=arm-linux-androideabi
  --sysroot=xxx/ndk/current/platforms/android-23/arch-arm/usr
  -fPIC -O3 -DNDEBUG  -shared -Wl,-soname,libMyTest.so -o libMyTest.so
  CMakeFiles/MyTest.dir/MyTest.cpp.o

It appears -L flag is not getting generated by cmake.

If I directly run the above command with -L flag, linking goes through fine and the shared library does get created.

Can someone please tell me what is it that I am missing?

Update:

I have tried exporting LIBRARY_PATH as well as LDFLAGS but both are being ignored.

compor
  • 2,239
  • 1
  • 19
  • 29
Peter
  • 11,260
  • 14
  • 78
  • 155
  • As far as I understand, CMake treats link directories added with `link_directories` or `target_link_directories` as hints for **direct linking** with libraries (`target_link_libraries`). Linking with proper `gcc` is responsibility of the toolchain/platform file, not of the `CMakeLists.txt`. – Tsyvarev Feb 07 '21 at 21:01
  • Thank you for your help. The link_directories is actually defined in toolchain file and is being used with -DCMAKE_TOOLCHAIN_FILE. – Peter Feb 08 '21 at 06:39
  • @Peter the export variable is LD_LIBRARY_PATH. Aside from that, when you manually executed clang, can you see the linker invocation by adding a `-v` option? Moreover, can you try the original compilation with the `-nostdlib` flag, see [this](https://stackoverflow.com/q/51173869/3048763) related post. Lastly, the `-lgcc` will try to find libgcc.so, is that shared library in the required dir along with its static version (i.e., libgcc.a)? – compor Feb 09 '21 at 11:25

0 Answers0