code builds cleanly with cmake, fails when added as an external project

扶醉桌前 提交于 2019-12-13 06:33:46

问题


Question summary.

I have a project A that I build using CMake, which compiles cleanly.

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.

The kind of errors I get.

Compiling B gives errors like this one

warning: rvalue references are a C++11 extension [-Wc++11-extensions]

when it starts compiling A (which again, is brought in by ExternalProject_Add).

Note that -std=c++11 is set in all involved CMakeList.txt files.

Note that I am also pulling a google project using ExternalProject_Add, but it does not cause any problems.

Some specifics from the CMakeLists.txt files involved.

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

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

回答1:


By default, CMAKE_BUILD_TYPE is empty, so all configuration-specific settings are omitted.

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


来源:https://stackoverflow.com/questions/40475495/code-builds-cleanly-with-cmake-fails-when-added-as-an-external-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!