[CMake] Enabling C99 in CMake

Rolf Eike Beer eike at sf-mail.de
Mon Jun 27 13:07:48 EDT 2011


Michael Hertling wrote:

> SET_SOURCE_FILES_PROPERTIES(${CMAKE_BINARY_DIR}/c89.c
>     PROPERTIES COMPILE_FLAGS ${CMAKE_C_DIALECT_C89})
> SET_SOURCE_FILES_PROPERTIES(${CMAKE_BINARY_DIR}/c99.c
>     PROPERTIES COMPILE_FLAGS ${CMAKE_C_DIALECT_C99})
> ADD_LIBRARY(c89 c89.c)
> ADD_LIBRARY(c99 c99.c)
> 
> It issues the supported C dialects and the contents of the associated
> dialect variables, and the c89.c and c99.c source files are compiled
> with the correct flags. Of course, the CMAKE_C_DIALECT_<DIALECT>
> variables can also be appended to the CMAKE_C_FLAGS[_<CONFIG>]
> variables instead of the COMPILE_FLAGS source file property.

With a bit of thinking I would suggest a slightly different way. Don't add 
specific compiler flags for that source file. Tell CMake that this sourcefile uses 
a specific language dialect:

SET_SOURCE_FILES_PROPERTIES(${CMAKE_BINARY_DIR}/c89.c
     PROPERTIES LANGUAGE_DIALECT C89)

Then let CMake internally do the fiddling with the compile flags. This would 
have a few benefits:

-if e.g. the language flags need to be passed first to the compiler CMake can 
assure this
-colliding flags can be detected by the underlying implementation
-if we ever get support for e.g. 2 different C compilers things like the Sun 
(was it Sun? Too lazy to look up) compilers can be supported as CMake would 
just call the right compiler binary
-this can easily change in any direction later if someone decides the 
underlying implementation needs a change

Well, basically all points target in the same direction, no? It seperates the 
switch logic from the user and allow greater flexibility in the implementation. 
With the expense that probably some C++ code of CMake needs to be touched.

Eike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110627/a7c9c54e/attachment.pgp>


More information about the CMake mailing list