'How do I change order of Libs in a cmake file?

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.



Solution 1:[1]

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

Solution 2:[2]

Aha! After much searching around and some trial and error I finally got it to work by adding the following lines to my cmake file CMakeLists.txt:

target_link_libraries(program
${GTK2_PKG_FLAGS}
)

And by using some of the advice from user2618142's answer I improved it. The function pkg_check_modules() doesn't work for me for some reason. I get Unknown CMake command "pkg_check_modules" but by using the following as my cmake file, things work as expected.

cmake_minimum_required(VERSION 2.6)

project(program)

exec_program(pkg-config ARGS --cflags gtk+-2.0 OUTPUT_VARIABLE GTK_FLAGS)
exec_program(pkg-config ARGS --libs   gtk+-2.0 OUTPUT_VARIABLE GTK_LIBS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GTK_FLAGS}")

add_executable(program
program.cpp
)

target_link_libraries(program
${GTK_LIBS}
)

Solution 3:[3]

To fix the error Unknown CMake command "pkg_check_modules",

Use below code at the top of CMakeLists.txt.

find_package(PkgConfig)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3 zrash