[CMake] Reading c-style preprocessor macros

Alexander Neundorf a.neundorf-work at gmx.net
Sun Dec 13 08:07:01 EST 2009


On Sunday 13 December 2009, Daniel Stonier wrote:
> I've been digging through the documentation, but I cannot seem to find
> anything which can read the value of a c style defined macro into a cmake
> variable.
>
> There's CheckSymbolExists, but that just flags the resulting cmake variable
> as true or false. It doesn't actually read the definition into the
> variable.
>
> I also checked FindBoost to see how they grab boost's version number -
> seems the do a file read and a regex on the resulting value.
>
> Is there an easy way to do this? I would have thought it would have been
> fairly common functionality - for instance, to gather information about
> posix options.

Either read and regex the headers, or build a small executable which will 
contain a string with the value you are interested in. Then you can use 
file(STRINGS) to read this value.
This is done by cmake e.g. to determine the compiler, the size of types 
(CheckTypeSize.cmake) and I think in some other places too.

If possible, you should try to avoid try_run() since this brings problems in 
case the package should be cross compiled some day by somebody.


Alex


More information about the CMake mailing list