[Cmake] SET_TARGET_PROPERTIES + DEFINE_SYMBOL

Brad King brad.king at kitware.com
Mon Jul 26 09:04:12 EDT 2004


ibiris at genius.org.br wrote:
> However, the first part of the symbol not appearing in the Makefile of 
> the library was right according to my experiments. I attach a tarball 
> with the code for a small test case that I tried on windows and repeated 
> this behaviour.
[snip]
> To build you ought to make this in the build directory mentioned above
> 
> cmake -DDEFSYM:BOOL=ON -G"NMake Makefiles" ..
> 
> this should activate a section in the src/CMakeLists.txt where the 
> SET_TARGET_PROPERTIES is invoked on the library target to define the 
> symbols __FOO__ and __BAR__ for the compilation of the library files. If 
> these symbols are defined then a -D__FOO__ should appear in the 
> generated Makefile for the src of the library. If __FOO__ is defined 
> then the test program will print a message saying so ...
> 
> But it does not in my tests. The generated src/Makefile does not define 
> this symbol for the files of the library target.

The code of interest looks like this:

ADD_LIBRARY(foobar_lib SHARED a.c )
IF (DEFSYM)
   ADD_LIBRARY(foobar_lib_alt SHARED a.c)
   SET_TARGET_PROPERTIES(foobar_lib_alt PROPERTIES
                         DEFINE_SYMBOL __FOO__
                         DEFINE_SYMBOL __BAR__)
ENDIF (DEFSYM)

There are two problems.  There is only one DEFINE_SYMBOL property, so 
setting it twice just erases the first one.  This property is meant for 
use only in helping libraries define export macros for DLLs, and only 
one macro is necessary for this purpose.  However, it would not be hard 
to modify CMake to support multiple definitions in this case.  Feel free 
to add this to the bug tracker at

   http://www.cmake.org/Bug

as a feature request.

The second and more important problem is that you are using the source 
file a.c in two libraries in the same directory.  Only one Makefile is 
generated, so it can contain only one rule for "a.o".  Therefore the 
rule for a.o that gets generated is coming from the first library and 
the DEFINE_SYMBOL property is not present.  The second target never 
generates this rule because CMake sees that there is already a rule for a.o.

There are a couple solutions to this problem:

1.) Reorganize your libraries so only one is needed.
2.) Create a separate directory in which the second library is built
     (it can always refer to the original sources by relative paths).
3.) For each source that you want in both libraries, say a.c, create
     an extra source like this:

/* a_alt.c */
#define __FOO__
#define __BAR__
#include "a.c"

Then add the second library using these sources.  Using the FOREACH and 
CONFIGURE_FILE commands you should be able to automate this process.

-Brad


More information about the Cmake mailing list