Mixing C and C ++ sources in cmake

I am trying to compile C and C ++ source codes. I am currently using the C ++ 0x standard for C ++ by adding this line to my cmake file add_definitions(-std=c++0x)

.

At compile time, I get the following warning: cc1: warning: command line option β€˜-std=c++11’ is valid for C++/ObjC++ but not for C [enabled by default]

and this error: error: β€˜for’ loop initial declarations are only allowed in C99 mode

refers to the for loop in the c code.

I am wondering how I can set a standard for c code in a cmake file.

+3


source to share


3 answers


You shouldn't use add_definitions

for this, instead do something like

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")

      



-std=c99

if you want to get the initial loop declarations.

add_definitions

for definitions such as -DUSE_SOME_LIBRARY

.

+7


source


Instead, add_definitions

you should use something like this:



set(CMAKE_CXX_FLAGS "-std=c++0x")

      

+1


source


You should first check if the compiler supports C ++ 11, see check_cxx_compiler_flag included in CheckCXXCompilerFlag macro

0


source







All Articles