[CMake] problem with CMake not including library's path (with pkg-config)

Rolf Eike Beer eike at sf-mail.de
Fri May 25 03:39:33 EDT 2018


Am 2018-05-24 18:48, schrieb Francesco Abbate:
> Hi all,
> 
> I stumbled in a problem with CMake. Everything is working fine except
> that, for two libraries that I locate with pkg-config, cmake does not
> include during linking the library's path (-L<path>) which is given by
> pkg-config.
> 
> 
> Here an extract of the CMakeLists.txt:
> 
> 
> [...]
> 
> include(FindPkgConfig)
> pkg_search_module(AGG REQUIRED libagg)
> 
> [...]
> 
> target_link_libraries(libcanvas ${AGG_LIBRARIES})
> target_include_directories(libcanvas PUBLIC
> ${PROJECT_SOURCE_DIR}/include ${AGG_INCLUDE_DIRS})
> [...]

Yes, this is a known shortcoming. But there is already help available!

For CMake >= 3.7 you can ask FindPkgConfig to create an imported target 
for you, which will then include not only the library paths, but also 
the include directories, so you do not need to explicitely call 
target_include_directories() anymore:

pkg_search_module(AGG REQUIRED IMPORTED_TARGET libagg)

target_link_libraries(libcanvas PkgConfig::AGG)

I strongly encourage you to go this way as it also drags in needed 
defines and the like.

If you can't go that route for whatever reason you need to do

link_directories(${AGG_LIBRARY_DIRS})

Side note: this is the only place I can still accept the usage of that 
command.

And since it's crappy that you have the full library path in the target, 
but not in a variable, there will be <PREFIX>_LINK_LIBARIES from CMake 
3.12 on, which would be the thing you pass to target_link_libraries() 
instead and which has the exact same information the imported target 
already has.

Greetings,

Eike


More information about the CMake mailing list