【发布时间】:2015-02-15 08:01:32
【问题描述】:
我一直在使用 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 教程正是我遇到的问题。它就像一种魅力,但似乎将许多看似未定义的变量混入其中。谁能解释这是如何以及为什么起作用的?
# 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})
【问题讨论】:
-
我不知道,但是当您得到答案时,请注意:您的包含路径和 pkg-config 行不匹配。你想使用 GTK+ 2 还是 GTK+ 3?这对于避免以后出现问题很重要。
-
谢谢。我修好了。我使用 2.0 从网站复制了该字符串,但忘记对其进行编辑以反映 3.0。
-
您在
PATH(环境)变量中有/usr/include?这一点用处都没有。 -
@EtanReisner,我写的时候一定是搞错了,可能是在考虑
/usr/local..。例如,我知道iostream位于/usr/include/c++..。如果这样的目录不在 PATH 中,编译器怎么知道在哪里可以找到这些头文件? -
PATH被 shell 用于查找二进制文件。编译器使用一组内置位置和-I标志指示的任何位置。