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

1

0

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 ""
)
Ritual answered 7/11, 2016 at 22:3 Comment(3)
So, project A has no -std=c++11 set when built via ExternalProject_Add. Probably, in that case the project has other build type than RELEASE or DEBUG. You may check that by output build type in the project A.Rotgut
This might be a newbie question, but doesn't ExternalProject_Add cause the added project to be configured with cmake? And since the external project has its own CMakeLists.txt, which sets -std=c++11, is that not sufficient?Ritual
@Rotgut I added CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release into the ExternalProject_Add part and now it works. If you write your advise as a clear response (not a comment) I will be able to accept it. Thanks!Ritual
R
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()
Rotgut answered 8/11, 2016 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.