用cmake干净地构建代码,当添加为外部项目时失败 [英] code builds cleanly with cmake, fails when added as an external project
问题描述
问题摘要.
我有一个使用CMake构建的项目A,可以干净地编译.
I have a project A that I build using CMake, which compiles cleanly.
但是,当我使用CMake的ExternalProject_Add命令将A拖入另一个项目B时,当编译到A点时,编译就会失败.
However, when I pull A into another project B using CMake's ExternalProject_Add command, compilation fails when it reaches the point of building A.
我遇到的那种错误.
编译B会出现这样的错误
Compiling B gives errors like this one
warning: rvalue references are a C++11 extension [-Wc++11-extensions]
开始编译A时(再次由ExternalProject_Add引入).
when it starts compiling A (which again, is brought in by ExternalProject_Add).
请注意,在所有涉及的CMakeList.txt文件中都设置了-std=c++11
.
Note that -std=c++11
is set in all involved CMakeList.txt files.
请注意,我也正在使用ExternalProject_Add拉google项目,但这不会引起任何问题.
Note that I am also pulling a google project using ExternalProject_Add, but it does not cause any problems.
所涉及的CMakeLists.txt文件中的一些细节.
以下摘录来自A的CMakeLists.txt:
The following excerpt is from A's CMakeLists.txt:
# Use the C++11 standard.
set (CC_FLAGS "-std=c++11")
# Figure out the warning flags to use.
CHECK_CXX_COMPILER_FLAG("-pedantic-errors" SUPPORTS_PEDANTIC_ERRORS)
CHECK_CXX_COMPILER_FLAG("-Wall" SUPPORTS_WALL)
CHECK_CXX_COMPILER_FLAG("-Wextra" SUPPORTS_WEXTRA)
if (SUPPORTS_PEDANTIC)
set (CC_FLAGS "${CC_FLAGS} -pedantic")
endif()
# [omitted]... similarly for the rest of the flags.
set (CMAKE_CXX_FLAGS_RELEASE "-O3 ${CC_FLAGS}")
set (CMAKE_CXX_FLAGS_DEBUG "-O0 -g ${CC_FLAGS}")
以下内容来自B的CMakeLists.txt.不同的部分在星号(*)之后.
The following is from B's CMakeLists.txt. The part that differs comes after the asterisks (*).
# Use the C++11 standard.
set (CC_FLAGS "-std=c++11")
# Figure out the warning flags to use.
CHECK_CXX_COMPILER_FLAG("-pedantic-errors" SUPPORTS_PEDANTIC_ERRORS)
CHECK_CXX_COMPILER_FLAG("-Wall" SUPPORTS_WALL)
CHECK_CXX_COMPILER_FLAG("-Wextra" SUPPORTS_WEXTRA)
if (SUPPORTS_PEDANTIC)
set (CC_FLAGS "${CC_FLAGS} -pedantic")
endif()
# [omitted]... similarly for the rest of the flags.
set (CMAKE_CXX_FLAGS_RELEASE "-O3 ${CC_FLAGS}")
set (CMAKE_CXX_FLAGS_DEBUG "-O0 -g ${CC_FLAGS}")
# ************* DIFFERS HERE ************
ExternalProject_Add (
projectA
PREFIX "${projectA_prefix}"
GIT_REPOSITORY "[omitted]"
INSTALL_COMMAND ""
)
ExternalProject_Add (
google_benchmark
PREFIX "${GoogleBenchmarkPrefix}"
GIT_REPOSITORY "https://github.com/google/benchmark.git"
UPDATE_COMMAND ""
BUILD_COMMAND make benchmark
INSTALL_COMMAND ""
)
推荐答案
通过默认, CMAKE_BUILD_TYPE 为空,因此所有特定于配置的设置被忽略.
By default, CMAKE_BUILD_TYPE is empty, so all configuration-specific settings are omitted.
这就是为什么没有使用任何CMAKE_CXX_FLAGS_*
变量的原因,因此该项目是在没有c++11
的情况下构建的.
That is why none of your CMAKE_CXX_FLAGS_*
variable is used, so the project is built without c++11
.
Exception is optimized keyword for target_link_libraries
: it works as corresponded to any non-debug config.
优良作法是为项目提供默认的构建类型. 这篇文章建议很好用于此目的的模板:
Good practice is to provide default build type for the project. This post suggests nice template for that purpose:
# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()
这篇关于用cmake干净地构建代码,当添加为外部项目时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!