我一直在使用 Clion IDE 并试图获得一个简单的 GTK 程序来使用它进行编译。我发现 Clion 使用 CMake,所以问题出在这里而不是 IDE 本身。我能够直接从终端成功编译和运行程序,但使用 CMake 却没有成功。
问题很简单:当我尝试编译时,编译器找不到位于/usr/include/gtk-3.0/gtk/gtk.h
中的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
有什么建议吗?
进一步的研究表明 this关于我遇到的问题的教程。它就像一个魅力,但似乎将许多看似 undefined variable 混合在一起。任何人都可以解释这是如何以及为什么有效的吗?
# 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})
最佳答案
使用 FindPkgConfig 模块
cmake_minimum_required(VERSION <your cmake version>)
project(myproject CXX)
# Find the GTK module using pkg-config
include(FindPkgConfig)
pkg_check_modules(GTK REQUIRED "gtk+-3.0")
# Add the path to its header files to the compiler command line
include_directories(${GTK_INCLUDE_DIRS})
# Add any compiler flags it requires
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GTK_CFLAGS} <other flags>")
# Add the makefile target for your executable and link in the GTK library
add_executable(${CMAKE_PROJECT_NAME} <list of source files>)
target_link_libraries(${CMAKE_PROJECT_NAME} ${GTK_LDFLAGS} <other libraries>)
人字形 (<...>
) 中的位需要替换为实际值
你可以通过
了解更多cmake --help-module FindPkgConfig
基本上是 include(FindPkgConfig)
line 引入了一些宏——它还确保 pkg-config 在环境中可用。然后调用 pkg_check_modules
有效运行 pkg-config
,解析输出,并使用第一个参数作为词干创建一组变量。
从帮助中,这是一个基本列表(XPREFIX 通常是您提供的词干)
<XPREFIX>_FOUND ... set to 1 if module(s) exist
<XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
<XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
<XPREFIX>_LDFLAGS ... all required linker flags
<XPREFIX>_LDFLAGS_OTHER ... all other linker flags
<XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
<XPREFIX>_CFLAGS ... all required cflags
<XPREFIX>_CFLAGS_OTHER ... the other compiler flags
<XPREFIX> = <PREFIX> for common case
<XPREFIX> = <PREFIX>_STATIC for static linking
关于c++ - 如何使用 CMake 添加编译器参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27514291/