CMake - link with both static and dynamic libraries

1

I have a CMake project that should run on very old ARM system. Since the system has different libc version then the toolchain one the only option is to link with libc statically.

The problem is that besides the libc the application should be linked with another dynamic library. The CMakeFiles.txt looks like the following:

target_link_libraries(${PROJECT_NAME} PUBLIC -static-libstdc++)
target_link_libraries(${PROJECT_NAME} PUBLIC -static-libgcc)

add_subdirectory(my_dynamic_library)
target_link_libraries(${PROJECT_NAME} PRIVATE my_dynamic_library)

But it looks that libc doesn't linked dynamically but as shared library:

readelf -d my_app

Dynamic section at offset 0x34668 contains 29 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [my_dynamic_library.so]
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

While running on the target system I get error:

./my_app: /lib/libc.so.6: version `GLIBC_2.17' not found (required by ./my_app)

I have no clue why this is happening but for some reason gcc linked my apps with wrong libc versions:

objdump -T ./my_app| grep 2.17
000103a8      DF *UND*  00000000  GLIBC_2.17  clock_gettime

I've read a lot of similar posts and I see that I have to add

target_link_libraries(${PROJECT_NAME} PUBLIC -static)

But that cause to another error:

../arm-none-linux-gnueabi/bin/ld: attempted static link of dynamic object my_dynamic_library.so

So I have no idea how to compile all that together.

c
cmake
arm
glibc
asked on Stack Overflow Jun 18, 2020 by folibis

1 Answer

0

Well, after a long search, I found a solution. Adding the following line:

target_link_libraries(${PROJECT_NAME} PUBLIC rt)

solves the issue. No -static option needed.

and yes, I have no idea why it works.

answered on Stack Overflow Jun 18, 2020 by folibis

User contributions licensed under CC BY-SA 3.0