gpt4 book ai didi

c++ - CMake 生成的带有系统和标准的 c++ 项目包括

转载 作者:太空宇宙 更新时间:2023-11-04 11:02:31 24 4
gpt4 key购买 nike

关于标题,我发现了很多讨论,但遗憾的是没有合适/通用的答案。对于 Eclipse CDT,可以全局设置 includes,但如果您的编译器发生变化,则必须重新设置。因此,我编写了以下 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)

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

最佳答案

我找到了一种方法来解决比 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)

关于c++ - CMake 生成的带有系统和标准的 c++ 项目包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26444845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com