CMake Pkg-Config Library Link Path

Robert Mason picture Robert Mason · Apr 9, 2012 · Viewed 8.3k times · Source

So I'm compiling the examples for libgstreamermm-0.10, and I've run into an issue with CMake.

With the version of libgstreamermm-0.10 that is installed by default on my system, the example segfaults. OK, so I got the latest sources and installed them to /usr/local and get the new example. Everything's looking good.

g++ main.cc player_window.cc -o test `pkg-config --cflags --libs gtkmm-3.0` `pkg-config --cflags --libs gstreamermm-0.10`

works fine and as expected. Great, now to try and get it to play nicely with CMake. I make a quick CMakeLists.txt file. I use pkg-config, as that worked fine and I don't really want to add a find module. So my file looks like this:

cmake_minimum_required(VERSION 2.6.2)
project(media_player_gtkmm)

INCLUDE(FindPkgConfig)

set(SOURCES main.cc player_window.cc)

add_executable(media_player_gtkmm ${SOURCES})

#dependencies
pkg_check_modules(GSTMM REQUIRED gstreamermm-0.10)
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
include_directories(${GTKMM_INCLUDE_DIRS} ${GSTMM_INCLUDE_DIRS})
link_directories(${GTKMM_LIBRARY_DIRS} ${GSTMM_LIBRARY_DIRS})
target_link_libraries(media_player_gtkmm ${GTKMM_LIBRARIES} ${GSTMM_LIBRARIES})

Everything compiles until the link stage, where I get undefined symbol errors. I then see the output of pkg-config --libs gstreamermm-0.10 starts with -L/usr/local/lib. I look at the output of make VERBOSE=1 and CMake is NOT adding the -L to the link command, even though I have the link_directories line. So even though I'm using the headers for the new version of gstreamer in /usr/local/include, the library from /usr/lib is being used instead, when I want the version in /usr/local/lib to be used. Pkg-Config seems to pick up on this, and adjusts accordingly, but CMake, even though I have it using pkg-config internally, doesn't pick up on the link flags.

I could manually set the link flags for now, but that seems like a bit of a hack. I'm sure there's a better way to specify this.

Answer

Anonymous picture Anonymous · Apr 9, 2012

link_directories only affects targets that come AFTER it. So, you need to move your add_executable to be after the link_directories call.