3

I have the following CMakeLists file:

################# # Com Project # ################# CMAKE_MINIMUM_REQUIRED(VERSION 2.6) SET(CMAKE_MODULE_PATH /home/loay/Desktop/V5) SET(CMAKE_CXX_FLAGS "-std=c++11") PROJECT (Sync) FIND_PACKAGE (OpenSplice REQUIRED) ##################### # Modele de donnees # ##################### SET (idls Communication.idl ) # Extraction des fichiers a partir du .idl execute_process(COMMAND idlpp -S -l cpp ./Communication.idl) FOREACH(idl ${idls}) OpenSplice_IDLGEN (${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1.cpp" VARS_1 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1.h" VARS_2 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1Dcps.cpp" VARS_3 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1Dcps.h" VARS_4 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1Dcps_impl.cpp" VARS_5 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1Dcps_impl.h" VARS_6 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1SplDcps.cpp" VARS_7 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "\\1SplDcps.h" VARS_8 ${idl}) STRING (REGEX REPLACE "\(.*\).idl" "ccpp_\\1.h" VARS_9 ${idl}) SET(OpenSplice_SYNC ${OpenSplice_SYNC} ${VARS_1} ${VARS_2} ${VARS_3} ${VARS_4} ${VARS_5} ${VARS_6} ${VARS_7} ${VARS_8} ${VARS_9}) ENDFOREACH(idl) ########################### # Fichiers de code source # ########################### SET (APP_SOURCES arduino.cpp Controller.cpp CheckStatus.cpp Controller.cpp ListenerReader.cpp main.cpp Publisher.cpp Subscriber.cpp rs232.c ) # Inclure dossier des sources des librairies a linker INCLUDE_DIRECTORIES( ${OpenSplice_INCLUDE_DIRS} ) # Inclure dossier des binaires des librairies a linker link_directories ( ${LIBRARY_OUTPUT_PATH} ) ############################# # Construction des binaires # ############################# SET (APP_EXE start) SET (SYNC Sync) ADD_EXECUTABLE (${APP_EXE} ${APP_SOURCES}) ADD_LIBRARY (${SYNC} SHARED ${OpenSplice_SYNC}) ADD_DEFINITIONS ( ${OpenSplice_DEFINITIONS} ${DEFINITIONS} ) TARGET_LINK_LIBRARIES (${SYNC} ${OpenSplice_LIBRARIES} ) TARGET_LINK_LIBRARIES (${APP_EXE} ${OpenSplice_LIBRARIES} ${SYNC} ) 

As you can see I added this line SET(CMAKE_CXX_FLAGS "-std=c++11") to enable support for ISO c++ 2011. I run this command cmake . -G "Unix Makefiles" to generate the files I need and to generate the makefile.

The problem is that when I run make command it fails with the following error:

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

This is weired because I did include the flag. What makes things even weirder is that when I re-run cmake . -G "Unix Makefiles" this problem is solved and I am able to build my files with no error!

What could be the cause of this problem ?

6
  • Which compiler (with version) are you using? Commented May 2, 2017 at 11:39
  • @PaulFloyd gcc 4.8.5 Commented May 2, 2017 at 11:40
  • OK, later versions of g++ default to C++ 14. 4.8.5 defaults to C++ 03. Commented May 2, 2017 at 11:42
  • @PaulFloyd thanks for the info Commented May 2, 2017 at 11:49
  • Do you have to support such ancient CMake as 2.6? Since version 3.1, CMake has the variable CMAKE_CXX_STANDARD (plus related CMAKE_CXX_STANDARD_REQUIRED and CMAKE_CXX_EXTENSIONS) which control this at native CMake level. With these, you wouldn't have to go through flags manually. Commented May 2, 2017 at 11:49

2 Answers 2

3

Probably it's being overwritten somewhere else, since this CMake file is not official from PrismTech. Change that command to this:

set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") 

and put it at the end of your cmake file.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked thanks! Why do I have to put it at the end of the file ? I tried putting it at the begging at first but it didn't work, why ?
@LoayAshmawy First, notice that your set command overwrites all previous CMAKE_CXX_FLAGS, unlike the one I provided, which appends to it. Like I said, this is not an official CMake file from PrismTech. Some guy created it. Probably he does exactly like you somewhere and overwrites CMAKE_CXX_FLAGS, which will effectively result in removing that c++11 flag you put at the beginning. When you add this to the end of the file, you'd be appending to whatever he did before.
2

There are differences when set compiler flags before or after project() call.

  1. When set flags before project() call you normally redefine default ones:

    set(CMAKE_CXX_FLAGS_INIT "<new-value>" CACHE STRING "Flags used by the compiler during all build types.") # ... some other assignments may be here ... project(<project-name>) 

    Note _INIT suffix for the variable: actually flags are initialized at the moment of project() call, and CMAKE_CXX_FLAGS_INIT provides default value for them.

  2. When set flags after project() call you normally append to default or user-supplied ones:

    project(<project-name>) # ... set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} <additional-value>") 

1 Comment

Thanks for the info !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.