Как добавить аргументы компилятора с помощью CMake?
Я использую Clion IDE и пытаюсь получить простую программу GTK для компиляции, используя ее. Я обнаружил, что Clion использует CMake, поэтому проблемы здесь, а не с самой IDE. Я могу успешно скомпилировать и запустить программу прямо из терминала, но мне не удалось использовать CMake.
Проблема проста: когда я пытаюсь скомпилировать, компилятор не может найти gtk.h, который находится в/usr/include/gtk-3.0/gtk/gtk.h
, Я обнаружил, что каким-то образом аргумент компилятора команды'pkg-config --libs --cflags gtk+-3.0'
исправляет эту проблему, но мне не удалось добавить этот аргумент с помощью CMake.
Я пытался:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} `pkg-config --libs --cflags gtk+-3.0`")
Но меня встретили:
Linking CXX executable test
c++: error: `pkg-config: No such file or directory
c++: error: gtk+-3.0`: No such file or directory
c++: error: unrecognized command line option ‘--libs’
c++: error: unrecognized command line option ‘--cflags’
make[3]: *** [test] Error 1
make[2]: *** [CMakeFiles/test.dir/all] Error 2
make[1]: *** [CMakeFiles/test.dir/rule] Error 2
make: *** [test] Error 2
Какие-либо предложения?
Дальнейшие исследования показалиэтот учебник именно по вопросу, который я имею. Это работает как шарм, но, кажется, бросает много, казалось бы, неопределенных переменных в микс. Кто-нибудь может объяснить, как и почему это работает?
# Set the name and the supported language of the project
project(hello-world C)
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 2.6)
# Use the package PkgConfig to detect GTK+ headers/library files
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
add_definitions(${GTK3_CFLAGS_OTHER})
# Add an executable compiled from hello.c
add_executable(hello main.c)
# Link the target to the GTK+ libraries
target_link_libraries(hello ${GTK3_LIBRARIES})