[CMake] if( defined xxx ) does not work as (I) expected

Brad King brad.king at kitware.com
Wed Oct 25 09:29:55 EDT 2006


Michael Hufer wrote:
> Hi all,
> I am new to CMake and am currently switching the make system for our X-Gen 
> product to CMake.
> Since the project contains a lot of shared libraries I wrote a macro to ease 
> the writing of CMakeLists.txt for them. The macro receives a list of 
> "external" libraries this library depends on. Some of these externals require 
> extra defines or link flags and I collect them in the macro like this:
> ---
> foreach( EXT ${EXTERNAL_LIBS} )
>   [...]
>   if( DEFINED ${EXT}_DEFINES )
>     set( EXT_DEFINES "${EXT_DEFINES} ${${EXT}_DEFINES}" )
>   endif( DEFINED ${EXT}_DEFINES )
>  [...]
> endforeach( EXT ${EXTERNAL_LIBS} )
> [...]
> if( DEFINED EXT_DEFINES )
>   set_source_files_properties( $MODULE_SOURCES} PROPERTIES COMPILE_FLAGS 
> ${EXT_DEFINES} )
> endif( DEFINED EXT_DEFINES )
> ---
> (the macro is defined in cmake/XGen-Macros.cmake in the attached zip-file).
> while the first 'if(defined ${EXT}_DEFINES)'  works as expected the second 
> 'if( DEFINED EXT_DEFINES )' where I check if actually any of the 'externals' 
> for this library needs extra defines is always false even if the message() 
> inserted before reports that this variable is in fact defined and shows its 
> value.
> The handling of extra link flags is identical and there also the first 
> 'if-defined' works as expected but the second does not...
> 
> I checked and double checked the macrocode, inserted debug messages etc. and 
> don't see any obvious errors. 
> So what am I doing wrong or did I actually find a bug in cmake?

I tried your example but had to strip a bunch of code since the source
files are not actually in the zip file.  I get the behavior you expect
from IF(DEFINED) in both cases.

-Brad


More information about the CMake mailing list