Cmake -std参数的文档

时间:2016-02-23 20:03:01

标签: cmake

我正在使用Clion,它使用Cmake,它使用如下所示的CMakeLists.txt:

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})

我一直在寻找-std=c99的解释。默认值为-std=c++11,我改变了它,因为我正在编写一个C程序,凭直觉,我没有在任何地方看到该关键字,我猜对了。但是,我不完全理解这个参数的作用,我想。还需要哪些其他参数?

该文件到底在哪里?

1 个答案:

答案 0 :(得分:2)

首先。如果您正在编写C程序,请将文件命名为.c - 而不是.cpp,以确保CMake将它们标识为C代码。这是您默认显示-std=c++11的原因。

然后您可以使用SET(CMAKE_C_FLAGS ...)添加编译器标志,即使这不是推荐的方式。你永远不会假设GCC会被召唤。

正如@zaufi所说。您可以使用:

set_property(TARGET Thesis PROPERTY C_STANDARD 99)

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

-std标志是GCC编译器标志,用于确定GCC应启用的语言功能。在这里阅读更多: 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.