2

We are switching to use cmake, and one of the applications I'm building use a library called "debug". (Say it's located at /path/to/libdebug.so).

I realized that following doesn't work as debug is a special keyword.

add_executable(myapp myapp.cpp)
target_link_libraries(myapp lib1 lib2 debug lib3)

Neither this works,

add_executable(myapp myapp.cpp)
target_link_libraries(myapp lib1 lib2 lib3 debug)

as it complains that

The "debug" argument must be followed by a library

Is there any workaround for this in cmake? For now to be able to continue working, I copied libdebug.so to libdebug2.so and I'm linking against debug2. But I need to find a long-term solution in cmake as libdebug.so is used by other projects too and I can't simply change it's name.

Cenkoloji
  • 93
  • 1
  • 5

2 Answers2

4

Another approach could be creating IMPORTED target for debug library:

find_library(DEBUG_LIBRARY debug PATH <directory-contained-the-library> NO_DEFAULT_PATH)
add_library(debug_lib SHARED IMPORTED)
set_target_properties(debug_lib PROPERTIES IMPORTED_LOCATION ${DEBUG_LIBRARY})

This target then may be used for link with:

target_link_libraries(myapp lib1 lib2 lib3 debug_lib)

While this approach requires more lines (and variables) than using full library name, it is platform independent: extension of the library searched is choosen automatically by CMake.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Aha that's a neat solution. Thanks! – Cenkoloji Sep 27 '16 at 07:19
  • Yeah I was thinking about this approach too but could not test if "debug" was a reserved word in there too. Seeing that this works, this is definitively the better approach. – Hayt Sep 27 '16 at 07:28
  • I'm attempting to use this approach to fix another issue and I'm getting "cannot find -ldebug_lib" while making. It gets compiled to libdebug.so, but that information isn't translated to the Makefile for some reason. – ijustlovemath May 04 '17 at 22:42
  • Unlike to normal library target, *IMPORTED* has **local visibility** by default. For be able to use this target in the global scope, you need to specify additional *GLOBAL* keyword when create the *IMPORTED* target. – Tsyvarev May 04 '17 at 22:54
  • Excellent! I'm now seeing a "Cannot generate a safe runtime search path" error for all targets which need this library. Everything built though, can i ignore this? I'd like to not ignore if possible. – ijustlovemath May 04 '17 at 23:09
  • This is unrelated problem, and deserve separate question. E.g., like [this one](http://stackoverflow.com/questions/22536567/cmake-cannot-resolve-runtime-directory-path). – Tsyvarev May 05 '17 at 07:22
1

In cmake you can also specify a library by it's full name. This is useful when you want to specify whether you want to use the static or dynamic library.

In you case this is a workaround the reserved word "debug".

target_link_libraries(myapp lib1 lib2 lib3 libdebug.so)

A disadvantage if this approach though is you run into issues if you want to compile things in Windows too. You need then to workaround this with a variable set to either the .so or the .lib file.

Hayt
  • 5,210
  • 30
  • 37