CMake生成的c ++项目与系统和标准包括 [英] CMake generated c++ project with system and standard includes

查看:150
本文介绍了CMake生成的c ++项目与系统和标准包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于标题,我发现了很多讨论,但不幸的是没有适当的/普遍的答案。
对于Eclipse CDT,可以在全局范围内设置include,但如果您的编译器更改,则必须重新执行。
因此,我编写了一个 CMakeFile.txt 的以下工作最小化示例来设置编译器使用的include。

 #检查是否需要CMake版本已安装
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

#设置项目名称到文件夹的名称
string(REGEX MATCH[^ /] + $PROJECT_NAME$ {CMAKE_CURRENT_BINARY_DIR})
消息(状态将PROJECT_NAME设置为$ {PROJECT_NAME})
项目($ {PROJECT_NAME})

#清除c ++编译器的标准包含路径
execute_process(COMMAND echo COMMAND $ {CMAKE_CXX_COMPILER} -Wp,-v -x c ++ - -fryntax-only ERROR_VARIABLE GXX_OUTPUT)
set(ENV {GXX_OUTPUT} $ {GXX_OUTPUT})
execute_process(COMMAND echo $ {GXX_OUTPUT} COMMAND grep^ \OUTPUT_VARIABLE GXX_INCLUDES)

#将目录添加到此目录的末尾包含路径
include_directories(
$ {GXX_INCLUDES}


#定义可执行文件名称和所需的源文件
add_executable($ {PROJECT_NAME}main.cpp)

包含的 grep 在a **中有一些痛苦,但它有效。
另外一点是,它不适用于 cmake 2.8.7 以上这个错误 http://public.kitware.com/Bug/view.php?id=15211
所以,我想知道有没有人有更好的方法来设置系统包括?

解决方案

我发现一个方法来解决它甚至更高版本的cmake比cmake 2.8.7。
关键是,我必须使用由; 分隔的列表。
当zaufi提到,当然,添加标准包括的可能性的建立,但这只能与你的标准环境一起工作,而不是与例如。交叉编译器环境。



所以这里是工作的 CMakeLists.txt

 #检查是否需要CMake版本
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

#将项目名称设置为
字符串的名称(REGEX MATCH[^ /] + $PROJECT_NAME$ {CMAKE_CURRENT_BINARY_DIR})
消息(状态将PROJECT_NAME设置为$ {PROJECT_NAME})
项目($ {PROJECT_NAME})

#清除c ++编译器的标准包含路径
execute_process(COMMAND echo COMMAND $ {CMAKE_CXX_COMPILER} -Wp,-v -x c ++ - -fsyntax - 只有ERROR_VARIABLE GXX_OUTPUT)
set(ENV {GXX_OUTPUT} $ {GXX_OUTPUT})
execute_process(COMMAND echo $ {GXX_OUTPUT} COMMAND grep^ \COMMAND sed's#\ ## g命令tr\\\
\\;OUTPUT_VARIABLE GXX_INCLUDES)

#将目录添加到此目录的末尾包含路径
include_directories(
$ {GXX_INCLUDES}


#定义可执行文件名称和所需的源文件
add_executable($ {PROJECT_NAME}main.cpp)


Regarding the title I found a lot of discussions but unfortunately no proper/universal answer. For Eclipse CDT one could set the includes globally, but if your compiler changes you have to do it again. Thus, I wrote the following working minimal example of a CMakeFile.txt to set the includes, which are used by the compiler.

# Check wheter required CMake Version is installed
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

# Set the project name to the name of the folder
string (REGEX MATCH "[^/]+$" PROJECT_NAME "${CMAKE_CURRENT_BINARY_DIR}")
message (STATUS "Set PROJECT_NAME to ${PROJECT_NAME}")
project ("${PROJECT_NAME}")

# Grep the standard include paths of the c++ compiler
execute_process(COMMAND echo COMMAND ${CMAKE_CXX_COMPILER} -Wp,-v -x c++ - -fsyntax-only ERROR_VARIABLE GXX_OUTPUT)
set(ENV{GXX_OUTPUT} ${GXX_OUTPUT})
execute_process(COMMAND echo ${GXX_OUTPUT} COMMAND grep "^\ " OUTPUT_VARIABLE GXX_INCLUDES)

# Add directories to the end of this directory's include paths
include_directories(
    ${GXX_INCLUDES}
)

# Defines executable name and the required sourcefiles
add_executable("${PROJECT_NAME}" main.cpp)

The greping of the includes is some pain in the a**, but it works. Another point is, that it does not work for cmake above cmake 2.8.7 concerning this bug http://public.kitware.com/Bug/view.php?id=15211 . So, I want to know if anyone have a better way to set the system includes?

解决方案

I found a way to solve it for even higher versions of cmake than cmake 2.8.7. The point is, I have to work with lists seperated by ;. As zaufi mentioned, there is of course the build in possibility to add the standard includes, but this do only work with you standard environment, not with e.g. cross compiler environments.

So here is the working CMakeLists.txt:

# Check wheter required CMake Version is installed
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

# Set the project name to the name of the folder
string (REGEX MATCH "[^/]+$" PROJECT_NAME "${CMAKE_CURRENT_BINARY_DIR}")
message (STATUS "Set PROJECT_NAME to ${PROJECT_NAME}")
project ("${PROJECT_NAME}")

# Grep the standard include paths of the c++ compiler
execute_process(COMMAND echo COMMAND ${CMAKE_CXX_COMPILER} -Wp,-v -x c++ - -fsyntax-only ERROR_VARIABLE GXX_OUTPUT)
set(ENV{GXX_OUTPUT} ${GXX_OUTPUT})
execute_process(COMMAND echo ${GXX_OUTPUT} COMMAND grep "^\ " COMMAND sed "s#\ ##g" COMMAND tr "\n" "\\;" OUTPUT_VARIABLE GXX_INCLUDES)

# Add directories to the end of this directory's include paths
include_directories(
    ${GXX_INCLUDES}
)

# Defines executable name and the required sourcefiles
add_executable("${PROJECT_NAME}" main.cpp)

这篇关于CMake生成的c ++项目与系统和标准包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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