Link order matters. I have observed that when I compile my program with:
gcc `pkg-config --cflags --libs gtk+-2.0` program.cpp -o program
which produces a number of linker errors: "undefined reference to `_gtk_init_abi_check' ", and others. This can be remedied by specificying the input file before the libraries.
gcc program.cpp `pkg-config --cflags --libs gtk+-2.0` -o program
My Question:
How can I fix a problem of this nature when I am using a Cmake file? Here are the contents of a simple cmake file I am currently using.
cmake_minimum_required(VERSION 2.6)
project(program)
add_executable(program
program.cpp
)
EXEC_PROGRAM(pkg-config ARGS --cflags --libs gtk+-2.0
OUTPUT_VARIABLE GTK2_PKG_FLAGS)
SET(GTK2_PKG_FLAGS CACHE STRING "GTK2 Flags" "${GTK2_PKG_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GTK2_PKG_FLAGS}")
Now when I do a cmake
followed by a make
I get the same linker errors that the first line above gives me, so I know my linker problems are strictly related to order. So how do I change the link order when using pkg-config in a cmake file? I've tried reordering parts of my cmake file, but I don't seem to be finding the right order.
You have passed both the arguments --cflags
and --libs
in the command which will give both -I and -L parts of the .pc file in one variable.
Try running message("${GTK2_PKG_FLAGS}")
to print the contents.
Hence it may not be prudent to link the complete variable $GTK2_PKG_FLAGS using target_link_libraries().
You may also want to try below steps
INCLUDE(FindPkgConfig)
pkg_check_modules(GTK REQUIRED gtk+-2.0)
#include
include_directories(${GTK_INCLUDE_DIRS})
#link
link_directories(${GTK_LIBRARY_DIRS})
target_link_libraries(program ${GTK_LIBRARIES})
Refer question