CMake生成的c ++项目包含系统和标准

时间:2014-10-18 21:42:42

标签: c++ linux eclipse include cmake

关于标题我发现了很多讨论,但遗憾的是没有正确/普遍的答案。 对于Eclipse CDT,可以全局设置包含,但如果您的编译器发生更改,则必须再次执行此操作。 因此,我编写了以下工作最小的CMakeFile.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 "^\ " 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)

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

1 个答案:

答案 0 :(得分:1)

我找到了一种方法来解决它比cmake 2.8.7更高版本的cmake。 关键是,我必须处理由;分隔的列表。 正如zaufi所提到的,当然有可能增加标准包括,但这只适用于你的标准环境,而不是例如交叉编译环境。

所以这是工作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)