Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake Pkg-Config Library Link Path

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.

like image 382
Robert Mason Avatar asked Apr 09 '12 17:04

Robert Mason


People also ask

Does CMake use pkg-config?

CMake can generate pkg-config . pc files for packages. The . pc file can be used by many build systems.

What is Pkgconfig in Linux?

pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line so an application can use gcc -o test test. c `pkg-config --libs --cflags glib-2.0` for instance, rather than hard-coding values on where to find glib (or other libraries).

What is Pkg_check_modules?

pkg_check_modules. Checks for all the given modules, setting a variety of result variables in the calling scope.

How does CMake Find_library work?

find_library tells CMake that we're looking for a library and once we have found it, to store the path to it in the variable LIBIMAGEPIPELINE_LIBRARY . Likely filenames are passed with NAMES LibImagePipeline . It is good practice to pass the names without the file extension like .


1 Answers

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

like image 194
Anonymous Avatar answered Oct 03 '22 03:10

Anonymous