2016-02-23 20 views
1

我使用的克利翁其採用CMake的它採用的CMakeLists.txt,看起來像這樣:的文檔CMake的-STD參數

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

'-std'是** **編譯標誌,而不是一個一個的CMake。 '-std = c99'表示要求編譯器使用[C99標準](https://en.wikipedia.org/wiki/C99)。 – Tsyvarev

+2

,如果你打算要求CMake 3.3最小化,最好使用['CMAKE_C_STANDARD'](https://cmake.org/cmake/help/v3.3/variable/CMAKE_C_STANDARD.html),也許[ CMAKE_C_STANDARD_REQUIRED'](https://cmake.org/cmake/help/v3.3/variable/CMAKE_C_STANDARD_REQUIRED.html)...或設置相應的目標屬性。 – zaufi

回答

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.