简体   繁体   中英

How to include `pkg-config --cflags --libs gtk+-2.0` in CXX_FLAGS of CMake

This is the 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 rather than Makefile. This part of the CMakeLists.txt file 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 have searched a lot but cannot find an answer. Please help.

If you are using find_package(Gtk2 ...), you don't have to use pkg-config at all. CMake will find the correct flags for you. Plus, this works for Operating Systems like Windows, where pkg-config is not present.

HOWEVER, if you insist on using pkg-config, do the following:

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()

This adds the output of 'pkg-config --cflags' into your CXX_FLAGS and also makes sure your executable is linked against the Gtk2 libraries from 'pkg-config --libs'

EDIT: If you don't mind some extra advice, the libraries that pkg-config gives you do not belong in 'CFLAGS'. There should be a special 'LIBS' (or any similar name) variable that holds the libraries you link against.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM