我一直在尋找便攜式方式來強制CMake啟用編譯器的C99功能,以避免以下
gcc錯誤:
error: ‘for’ loop initial declarations are only allowed in C99 mode for (int s = 1; s <= in_para->StepNumber; s++){ ^
我也不想檢查哪個編譯器並附加如下:
set(CMAKE_C_FLAGS "-std=c99") # that would be bad
所以我發現這個帖子:Enabling C99 in CMake和相關的功能要求:0012300: CMake has no cross-platform way to ask for C99.在這個螳螂錯誤中,我了解了target_compiler_features
,之后我發現了這些SOF答案:How to activate C++11 in CMake?和How to detect C++11 support of a compiler with CMake.
所以我的問題是:這個target_compiler_features將提供一種要求C功能和C功能的方法?現在最流行的方式是什么 – 我正在使用CMake 2.8.12.2. target_compiler_features不在CMake的最新版本(3.0.0)中.你知道什么時候被釋放嗎?
創建目標(如庫或可執行文件)后,將這樣的行放在您的CMakeLists.txt文件中:
set_property(TARGET tgt PROPERTY C_STANDARD 99)
其中tgt是您的目標的名稱.
我認為這是添加在CMake 3.1中,文檔在這里:
http://www.cmake.org/cmake/help/v3.1/prop_tgt/C_STANDARD.html
如果您需要支持早於3.1的CMake版本,則可以使用此宏:
macro(use_c99)
if (CMAKE_VERSION VERSION_LESS "3.1") if (CMAKE_C_COMPILER_ID STREQUAL "GNU") set (CMAKE_C_FLAGS "--std=gnu99 ${CMAKE_C_FLAGS}") endif () else () set (CMAKE_C_STANDARD 99) endif () endmacro(use_c99)
將該宏放置在頂級文件中,以便隨處可見,您可以在任何使用C99代碼定義目標的CMakeLists文件的頂部寫入use_c99().