Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMAKE linking to system library

Tags:

c++

c

cmake

We would like to build a shared library with CMAKE system. It's something like:

lib/
  CMakeLists.txt
  src/
    CMakeLists.txt
    module/
      CMakeLists.txt
      module1.c
    foo.c

module1.c needs some standard shared library like librt. We have

add_library(module module1.c)
target_link_libraries(module rt)

in the module/ sub-directory. But still getting errors that stuff from librt is not known.

like image 418
Cartesius00 Avatar asked Oct 09 '11 10:10

Cartesius00


2 Answers

If librt is linked with -lrt, then in CMake you can link it in this way:

target_link_libraries(module -lrt)
like image 169
Maciej Avatar answered Oct 12 '22 22:10

Maciej


Actually what you are doing should work. If the compiler cannot find the library it may simply not be in the standard library paths. You can use the following help to specify different link_directories (-L/some/path parameter to the compiler).

cmake --help-command link_directories

Another useful thing you can do while debugging CMake builds is print out the commands it runs the compiler with:

make VERBOSE=1

You can then take the commands and manually tailor them to work. After that it is a matter of modifying CMAKE_C_FLAGS/include_directories/link_directories

like image 30
RushPL Avatar answered Oct 12 '22 21:10

RushPL