Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link static library using CMake

I cannot get the library ${static_library} to statically link using CMake. I have tried hard coding the path and as far as I'm aware TARGET_LINK_LIBRARIES should link this in automatically.

ADD_LIBRARY(libraryA STATIC ${source_files})
TARGET_LINK_LIBRARIES(libraryA debug ${static_library})

There is a sub project that links libraryA later in the build

ADD_EXECUTABLE(testA ${test_sources}) 
TARGET_LINK_LIBRARIES(testA libraryA)

which has ${static_library} correctly linked in but I need ${static_library} to be directly linked into libraryA.

like image 992
Steve Avatar asked Apr 21 '26 21:04

Steve


2 Answers

You cannot link libraries into libraries directly.

Some compilers allow you to do this (e.g. MS Visual Studio has an option), but others don't, which is why CMake also won't allow it. See this answer for what your options are to deal with this.

like image 192
ComicSansMS Avatar answered Apr 23 '26 10:04

ComicSansMS


target_link_libraries(my_target PRIVATE lib_name${CMAKE_STATIC_LIBRARY_SUFFIX})

I have libpq.a and libpq.so (postgres) and with this solution it links to the static library so I can execute my_target on a clean OS.

like image 23
Oleg Avatar answered Apr 23 '26 10:04

Oleg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!