Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing C and C++ sources in cmake [duplicate]

Tags:

c++

c

c++11

cmake

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

During the compilation, 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 referred to a for loop in the c code.

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

like image 425
MinaKamel Avatar asked Jan 05 '15 21:01

MinaKamel


2 Answers

You should not use add_definitions for that, 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 loop initial declarations.

add_definitions is for definitions like -DUSE_SOME_LIBRARY.

like image 61
Iharob Al Asimi Avatar answered Oct 26 '22 06:10

Iharob Al Asimi


Instead of add_definitions you should use something like this instead:

set(CMAKE_CXX_FLAGS "-std=c++0x")
like image 32
Trent Avatar answered Oct 26 '22 04:10

Trent