Forcing C99 in CMake (to use 'for' loop initial declaration)

cmake_cxx_standard
cmake -c
cmake cflags
cmake c18
cmake cmake_c_standard
c99 vs c11
cmake pass compiler flags
cmake_c_standard 11

I've been searching a portable way to force CMake to enable the compiler's C99 features in order to avoid the following gcc error for instance:

error: ‘for’ loop initial declarations are only allowed in C99 mode
for (int s = 1; s <= in_para->StepNumber; s++){
^

I also wouldn't like to check for which compiler and append something like:

set(CMAKE_C_FLAGS "-std=c99") # that would be bad

So I found this post: Enabling C99 in CMake and the associated feature request: 0012300: CMake has no cross-platform way to ask for C99. In this Mantis bug I learned about target_compiler_features and after that I found these SOF answers on it: How to activate C++11 in CMake? and How to detect C++11 support of a compiler with CMake.

So my questions are: this target_compiler_features will provide a way to require a C feature as well as a C++ one? What is the most portable way to achive this by now - I'm currently using CMake 2.8.12.2. The target_compiler_features isn't in CMake's most recent release version (3.0.0). Do you know when it is being released?

C_STANDARD, The C standard whose features are requested to build this target. flag being added for the compiler in use, a previous standard flag will be added instead. After putting that macro in your top-level file so it is visible everywhere, you can just write use_c99 () at the top of any CMakeLists file that defines a target with C99 code in it. CMake issue #15943 for clang users targeting macOS

Edit CMakeLists

add on line > 2

set (CMAKE_C_STANDARD 99)

then

cmake 3 ..

Forcing C99 in CMake (to use 'for' loop initial declaration), I've been searching a portable way to force CMake to enable the compiler's C99 features in order to avoid the following gcc error for instance: error: 'for' loop  set (CMAKE_C_FLAGS "-std=c99") However, that's the GNU way to enable C99, and other compilers do it differently. It would be nice if there were some flags for this set for you in whatever platform file you're using, e.g.: set (CMAKE_C_FLAGS "$ {CMAKE_ENABLE_C99}")

Add the following to your CMakeLists.txt file and run cmake again

set(CMAKE_C_FLAGS "std=c99")

Enabling C++11 And Later In CMake, The simplest way to use a particular C++ standard in your project is to add the following two variable definitions before you define any targets: Finally to force the use of the LLVM binutils, set the internal variable _CMAKE_TOOLCHAIN_PREFIX. This variable is honored by the CMakeFindBinUtils module: $ cmake -D_CMAKE_TOOLCHAIN_PREFIX=llvm- ..

In libevent, add the following in CMakeLists.txt

set (CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")

CMakeLists.txt - boringssl, CMakeLists.txt the CMake generators prefer gcc, even on Windows. set(​CMAKE_GENERATOR_CC "C4800" # 'int' : forcing value to bool 'true' or 'false' add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen. endif(). 25 Forcing C99 in CMake (to use 'for' loop initial declaration) Jul 19 '14 14 Linking different libraries for Debug and Release builds in Cmake on windows? Aug 16 '16

CMake Cookbook: Building, testing, and packaging modular software , Defer enabling C and CXX languages. project(BoringSSL NONE) the CMake generators prefer gcc, even on Windows. Use "symbol_prefix_include" to store generated header files "C4800" # 'int' : forcing value to bool 'true' or 'false'. CMake configures the entire project using a build format of your choice, meaning it sets everything up so that you can use, for example, Visual Studio to compile. By default (at least on linux), it makes a project that uses Make instead.

CrossCompiling · Wiki · CMake / Community · GitLab, Building, testing, and packaging modular software with modern CMake Radovan Bast, Roberto Di Remigio Note the use of the LOG_INSTALL option to also log the install step to file: internally built Boost installation root" FORCE ) set( BOOST_INCLUDEDIR ${BOOST_ROOT}/include CACHE PATH We declare a C99  CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 available, you can just write this in your top-level CMakeLists.txt file, or put it right before any new target is defined:

cmake: support old Cmake for -std=c99 · Issue #462, Note: This content has been superseded by the cmake-toolchains(7) manual. Expand to see original content. Cross compiling is supported by  I can't find any info on it, but only the other way around (e.g., how to set CMake to use clang). I've installed gcc-4.8 using brew, setup all dependencies, headers, etc, and now CMake refuses to use gcc. I've set my bash profile with both aliases and actual entries:

Comments
  • What's so bad about -std=c99? That's the way I'd do it. You probably don't have more than two different ways you need to support for different compilers (e.g. Clang and GCC are the same).
  • I would like something that would work for both gcc and MSVC without having to check for CMAKE_C_COMPILER_ID. By the way, I also use -std=c99, but it seems that there is / there will be a better approach with target_compiler_features.
  • Maybe someday there will be something better. I'm not sure what your question is then. This isn't a release-date forecasting site for CMake.
  • Well, I was wondering if there would be a chance some cmake dev notices this question. Also, as the bug report I cite is already three years old I was also hoping that someone have already figured out some better approach. Also, there are some answers on this future feature but only dealing with C++11, so I'm asking if it will apply also to C (as I'm not willing to read the code and/or draft documentation of it).
  • As far as I know MSVC simply doesn't implement C99.
  • @SeanBurton good point, at the same time, I think a lot of people use SO like I do, skipping directly to the answers, so FWIW I found the answer here, not in OP :) (of course I would have scrutinized OP eventually!)
  • Please explain in more detail, why that is the correct answer.
  • Perfect, this solved my problem!