[CMake] Can not get C++ 11 support enabled

Petr Kmoch petr.kmoch at gmail.com
Tue Jul 11 03:18:19 EDT 2017


As others have suggested, moving the add_executable() after the set() calls
is the correct solution.

The reason is that variables like CMAKE_CXX_STANDARD are used to
pre-initialise a target's properties when the target is created. In other
words, at the time add_executable() is called, CMake will inspect variables
like CMAKE_CXX_STANDARD and use them to initialise the target's properties
like CXX_STANDARD. It's the properties which really control the target's
build. See
https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html and
https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html for details.

The same applies to add_compile_options(). From its docs (
https://cmake.org/cmake/help/latest/command/add_compile_options.html ):
"Adds options to the compiler command line for targets in the current
directory and below that are added after this command is invoked"

But Craig is right that you shouldn't add `-std` directly, but use
CXX_STANDARD and CXX_EXTENSIONS instead.

Petr

On 11 July 2017 at 07:21, Craig Scott <craig.scott at crascit.com> wrote:

> As well as moving the add_executable() call after you set the various
> CMAKE_CXX... variables, you should also replace the call to
> add_compile_options(-std=c++11) with the following:
>
> set(CMAKE_CXX_EXTENSIONS OFF)
>
>
> On Tue, Jul 11, 2017 at 3:11 PM, Michael Ellery <mellery451 at gmail.com>
> wrote:
>
>>
>> > On Jul 10, 2017, at 10:07 PM, Florian Lindner <mailinglists at xgm.de>
>> wrote:
>> >
>> > Hello,
>> >
>> > my complete cmake file looks like that:
>> >
>> > cmake_minimum_required (VERSION 3.1)
>> > project(Preallocation)
>> > add_executable(prealloc prealloc_parallel.cpp)
>> >
>> > set(CMAKE_CXX_STANDARD 11)
>> > set(CMAKE_CXX_STANDARD_REQUIRED ON)
>> > add_compile_options(-std=c++11)
>> >
>> > find_library(petsc petsc
>> >  PATHS $ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib)
>> > if(NOT petsc)
>> >  message(FATAL_ERROR "petsc was not found")
>> > endif()
>> > target_link_libraries(prealloc ${petsc})
>> >
>> > find_package(Boost 1.60.0
>> >  REQUIRED
>> >  COMPONENTS program_options)
>> > target_link_libraries(prealloc ${Boost_LIBRARIES})
>> >
>> > find_package(MPI
>> >  REQUIRED)
>> > target_link_libraries(prealloc ${MPI_LIBRARIES})
>> >
>> > set(COMPILE_FLAGS  ${COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
>> > set(LINK_FLAGS ${LINK_FLAGS} ${MPI_LINK_FLAGS})
>> >
>>
>> I would try moving the add_executable() to the end of the CMakeLists file
>> (after the set() and find_library calls…)
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170711/e93485b2/attachment.html>


More information about the CMake mailing list