如何使用CMake添加编译器参数? [英] How to add compiler arguments using CMake?

查看:2773
本文介绍了如何使用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`)

但是, :

 链接CXX可执行文件测试
c ++:错误:`pkg-config:没有这样的文件或目录
c ++:error:gtk + -3.0`:没有这样的文件或目录
c ++:错误:无法识别的命令行选项'--libs'
c ++:错误:无法识别的命令行选项'--cflags'
make [3]:*** [test]错误1
make [2]:*** [CMakeFiles / test.dir / all]错误2
make [1]:** * [CMakeFiles / test.dir / rule]错误2
make:*** [test]错误2

有什么建议吗?




进一步的研究显示这个教程正是关于我遇到的问题。它的作用像一个魅力,但似乎将许多看似未定义的变量混合在一起。任何人都可以解释如何和为什么这个工程?

 #设置项目的名称和支持的语言
项目(hello-world C)
#设置构建这个项目所需的cmake的最低版本
cmake_minimum_required(VERSION 2.6)
#使用包PkgConfig检测GTK +头文件/库文件
find_package(PkgConfig必需)
pkg_check_modules(GTK3 REQUIRED gtk + -3.0)
#设置CMake使用GTK +,告诉编译器在哪里查找头文件
#并链接到哪里查找库
include_directories($ {GTK3_INCLUDE_DIRS })
link_directories($ {GTK3_LIBRARY_DIRS})
#将其他标志添加到编译器
add_definitions($ {GTK3_CFLAGS_OTHER})
#添加一个从hello.c编译的可执行文件
add_executable(hello main.c)
#将目标链接到GTK +库
target_link_libraries(hello $ {GTK3_LIBRARIES})


使用FindPkgConfig模块

  cmake_minimum_re quird(VERSION<您的cmake版本>)
项目(myproject CXX)

#使用pkg-config查找GTK模块
include(FindPkgConfig)
pkg_check_modules GTK需要gtk + -3.0)

#将其头文件的路径添加到编译器命令行
include_directories($ {GTK_INCLUDE_DIRS})

#添加任何编译器标志它需要
set(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} $ {GTK_CFLAGS}<其他标志>)

#为GTK库中的可执行文件和链接添加makefile目标
add_executable($ {CMAKE_PROJECT_NAME}<源文件列表>
target_link_libraries($ {CMAKE_PROJECT_NAME} $ {GTK_LDFLAGS}< other libraries>)

V形中的位(< ...> )需要用真实值替换



您可以通过

  cmake --help-模块FindPkgConfig 

基本上, include(FindPkgConfig) line会引入一些宏 - 它也确保pkg-config在环境中可用。然后,调用 pkg_check_modules 可以有效地运行 pkg-config ,解析输出并使用第一个变量创建一组变量参数作为干。



在帮助中,这是一个基本列表(XPREFIX通常是您提供的词干)

 < XPREFIX> _FOUND ...如果模块存在,则设为1 
< XPREFIX> _LIBRARIES ...只有库(不包括' -l')
< XPREFIX> _LIBRARY_DIRS ...库的路径(没有'-L')
< XPREFIX> _LDFLAGS ...所有必需的链接器标志
< XPREFIX> _LDFLAGS_OTHER ...所有其他链接器标志
< XPREFIX> _INCLUDE_DIRS ...'-I'预处理器标志(没有'-I')
< XPREFIX> _CFLAGS ...所有必需的cflags
< XPREFIX> _CFLAGS_OTHER ...其他编译器标志



< XPREFIX> =< PREFIX>对于常见情况
< XPREFIX> =< PREFIX> _STATIC for static linking


I have been using the Clion IDE and am trying to get a simple GTK program to compile using it. I have found that Clion uses CMake, so the issues is here rather than with the IDE itself. I am able to successfully compile and run the program directly from the terminal but have been unsuccessful using CMake.

The problem is simple: when I attempt to compile, the compiler cannot find gtk.h, which is located in /usr/include/gtk-3.0/gtk/gtk.h. I have found that somehow the command compiler argument 'pkg-config --libs --cflags gtk+-3.0' fixes this problem but I have been unable to add this argument using CMake.

I have tried:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} `pkg-config --libs --cflags gtk+-3.0`")

But am met with:

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

Any suggestions?


Further research has revealed this tutorial precisely on the issue I am having. It works like a charm but appears to throw many seemingly undefined variables into the mix. Can anyone explain how and why this works?

# 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})

解决方案

Use the FindPkgConfig module

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

the bits in chevrons (<...>) need to be replaced with real values

you can find out more with

cmake --help-module FindPkgConfig

Basically the include(FindPkgConfig) line brings in a few macros - it also ensures that pkg-config is available in the environment. Then a call to pkg_check_modules effectively runs pkg-config, parses the output, and creates a suite of variables using the first argument as a stem.

From the help, this is a basic list (XPREFIX is usually the stem you supply)

      <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

这篇关于如何使用CMake添加编译器参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆