[CMake] Building the same library with different defines
Brad King
brad.king at kitware.com
Wed Oct 4 15:34:31 EDT 2006
Patrick J. Franz -- ML wrote:
>
> I'm a fairly new CMake user, so I apologize in advance if this question
> is answered trivially elsewhere. I searched through the docs and Google
> already to no avail.
>
> In a project I am working on, I need to build our Win32 libraries in
> both cdecl and stdcall variants. The calling conventions can be
> controlled through a -D variable, but I am having a hard time getting
> CMake to modify definitions between builds of the same set of source files.
>
> I have tried 2 techniques:
>
> 1) Use SET_SOURCE_FILES_PROPERTIES() to change the -D settings between
> builds.
>
> # cdecl (default) build
> ADD_LIBRARY(my_lib SHARED src/lib.c)
>
> SET_SOURCE_FILES_PROPERTIES(src/lib.c COMPILE_FLAGS -DTURN_ON_STDCALL)
> ADD_LIBRARY(my_lib_stdcall SHARED src/lib.c)
>
> On my machine, SET_SOURCE_FILE_PROPERTIES() has the effect of setting
> TURN_ON_STDCALL for every build that uses src/lib.c.
>
>
> 2) Use ADD_DEFINITIONS() and REMOVE_DEFINITIONS().
>
> REMOVE_DEFINITIONS(-DTURN_ON_STDCALL)
> ADD_LIBRARY(my_lib SHARED src/lib.c)
>
> ADD_DEFINITIONS(-DTURN_ON_STDCALL)
> ADD_LIBRARY(my_lib_stdcall SHARED src/lib.c)
>
> The effect of this was no different then when I used
> SET_SOURCE_FILES_PROPERTIES().
>
> If I had to guess, I would suppose that lib.obj is really only being
> built once and simply linked twice to create the two libraries I want.
> What I really need is to force lib.c to be built (and linked) twice with
> the different -D options. Any suggestions on how I might do this?
It is being built twice, but the ADD/REMOVE_DEFINITIONS commands are
per-directory and take effect only at the bottom of each CMakeLists.txt
file. Really the state should be copied by ADD_LIBRARY so your code
would work as expected but there are historical reasons for the current
behavior. Source file properties have the same problem.
You need to use the more modern per-target options. Look at
SET_TARGET_PROPERTIES. There is a per-target COMPILE_FLAGS option in
which you can add the -D option.
-Brad
More information about the CMake
mailing list