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.sc...@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 <mellery...@gmail.com>
> wrote:
>
>>
>> > On Jul 10, 2017, at 10:07 PM, Florian Lindner <mailingli...@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…)
>>
>
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to