How to enable `pkg-config -cflags -libs gtk + -2.0` in CXX_FLAGS from CMake

These are CFLAGS in the Makefile.

CFLAGS = -I/usr/include/libglade-2.0 -I/usr/include/gsl `pkg-config --cflags --libs gtk+-2.0` -lglade-2.0 -lglut -I/usr/local/include/dc1394 -ldc1394

I want to use CMAKE, not Makefile. This is the part of the CMakeLists.txt file that I wrote.

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED "gtk+-2.0")

# Add the path to its header files to the compiler command line
include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})

# Add any compiler flags it requires
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GTK_CFLAGS}")

# Add the makefile target for your executable and link in the GTK library
target_link_libraries(${CMAKE_PROJECT_NAME} ${GTK_LIBRARIES})

# gtk and glade
find_package(GTK2 2.10 REQUIRED gtk glade)
if(GTK2_FOUND)
   include_directories(${GTK2_INCLUDE_DIRS})
   target_link_libraries(${CMAKE_PROJECT_NAME} ${GTK2_LIBRARIES})
endif()

My question is how to combine

`pkg-config --cflags --libs gtk+-2.0` 

in CXX_FLAGS. I searched a lot, but I can not find the answer. Please, help.

+4
source share
1 answer

If you use find_package (Gtk2 ...), you do not need to use pkg-config at all. CMake will find the right flags for you. It also works for operating systems such as Windows, where pkg-config is missing.

HOWEVER, if you insist on using pkg-config, follow these steps:

find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
  pkg_check_modules(GTK "gtk+-2.0")
  if (GTK_FOUND)    
    target_link_libraries(yourexecutable ${GTK_LIBRARIES})
    add_definitions(${GTK_CFLAGS} ${GTK_CFLAGS_OTHER})
  endif()
endif()

"pkg-config -cflags" CXX_FLAGS, , Gtk2 "pkg-config -libs"

EDIT: , , pkg-config , "CFLAGS". "LIBS" ( ), , .

+3

Source: https://habr.com/ru/post/1606827/


All Articles