dimitris93
dimitris93

Reputation: 4273

Documentation for Cmake -std parameters

I am using Clion which uses Cmake which uses a CMakeLists.txt that looks like this:

cmake_minimum_required(VERSION 3.3)
project(Thesis)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c99")

set(SOURCE_FILES main.cpp graph.h graph.c shared.h shared.c)
add_executable(Thesis ${SOURCE_FILES})

I have been searching and searching for an explanation on -std=c99. The default was -std=c++11 and I changed it because I am writting a C program, by intuition, I didn't see that keyword anywhere, I just guessed it. However, I don't fully understand what this parameter does, and I want to. What other parameters can it take ?

Where is that documentation exactly ?

Upvotes: 1

Views: 680

Answers (1)

Emil
Emil

Reputation: 516

First of all. If you are writing a C program, name your files .c - not .cpp to be sure that CMake identifies them as C code. This is the reason you see -std=c++11 by default.

You can then use SET(CMAKE_C_FLAGS ...) to add compiler flags even though this isn't the recommended way. You shall never assume that GCC will be called.

As @zaufi said. You can use:

set_property(TARGET Thesis PROPERTY C_STANDARD 99)

https://cmake.org/cmake/help/v3.1/prop_tgt/C_STANDARD.html#prop_tgt:C_STANDARD

The -std flag is a GCC compiler flag that determine which language features GCC shall enable. Read more here: http://linux.die.net/man/1/gcc

-std=
    Determine the language standard.
    This option is currently only supported when compiling C or C ++ .

    The compiler can accept several base standards, such as c89 or c++98, 
    and GNU dialects of those standards, such as gnu89 or gnu++98. By 
    specifying a base standard, the compiler will accept all programs 
    following that standard and those using GNU extensions that do not 
    contradict it. For example, -std=c89 turns off certain features of 
    GCC that are incompatible with ISO C90, such as the "asm" and 
    "typeof" keywords, but not other GNU extensions that do not have a 
    meaning in ISO C90, such as omitting the middle term of a "?:" 
    expression. On the other hand, by specifying a GNU dialect of a 
    standard, all features the compiler support are enabled, even when 
    those features change the meaning of the base standard and some 
    strict-conforming programs may be rejected.

Upvotes: 2

Related Questions