Unable to use c99 mode

I have my cmake flags configured this way:

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

But when compiling, i get the following error:

    ... loop initial declarations are only allowed in C99 mode

How do i fix this?

4 comments
Comment actions Permalink

Andrey,
by default new project are generated with C++11 standard.
To use C99 with C-code, add the following:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")

and make sure the corresponding files are c, not c++.

0
Comment actions Permalink

I have the same issue, in my C projet I have this in CMakeLists.txt:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11 -Wall")

But when I compile this from CLion this:

for (int i=1; i<data->filesize_in; i++) {

I get this:

Building C object CMakeFiles/MYPROJECT.dir/src/main.c.o
/projects/MYPROJECT/src/main.c: In function ‘main’:
/projects/MYPROJECT/src/main.c:58:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
  for (int i=1; i<data->filesize_in; i++) {
  ^


But, if I compile the project with CMake/make from the command line, there are no errors.

I have CLion 1.0.3.

0
Comment actions Permalink

You might have misprint in CMakeLists.txt. Should be

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -Wall")


0
Comment actions Permalink

You are right. Thanks Maria!

0

Please sign in to leave a comment.