[Cmake] Defining a variable for the compiler in one directory
only
Andy Cedilnik
andy . cedilnik at kitware . com
22 Sep 2003 07:48:40 -0400
Hi David,
First of all, you can do what you want by setting source file property
for each source file:
SET(foo_sources foo1.c ...)
SET_SOURCE_FILES_PROPERTIES(${foo_sources} PROPERTIES COMPILE_FLAGS
-DFOO)
and then the same in bar.
That said, using -Dsomething is bad. Here is why:
Let say that you create a library CoolLibrary, and half of your source
files depend on -DSomeWeirdFlag. Furthermore, you use that to enble some
feature, so header files also need this flag. The result is that
everybody that uses your library will have to specify this flag.
Suddenly your thing depends on your build process and is not portabile
any more.
The prefered way is to configure a header file that you then include
where needed. So, you create file let say:
CoolConfigure.h.in
and put in:
#cmakedefine SomeWeirdFlag
Then in your CMakeLists.txt you put:
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/CoolConfigure.h.in
${CMAKE_CURRENT_BINARY_DIR}/CoolConfigure.h)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
and you use it in your project. And anybody else that uses your library
will not have to know anything about the compile flags.
Andy
On Fri, 2003-09-19 at 18:42, Karr, David wrote:
> I want to generate projects in two directories like this:
>
> foo/foo.dsp
> foo/bar/bar.dsp
>
> I want all files compiled by foo.dsp to be compiled with
> the FOO variable defined, but *not* the BAR variable.
> I want bar.dsp to compile files with the BAR variable
> defined, but *not* the FOO variable.
>
> What I did naively at first was to put the following
> in foo/CMakeLists.txt:
>
> ADD_DEFINITIONS(-DFOO)
>
> And I put this in foo/bar/CMakeLists.txt:
>
> ADD_DEFINITIONS(-DBAR)
>
> This does exactly what I want in foo.dsp, but unfortunately
> bar.dsp is compiling all its files with both FOO and BAR
> defined, which causes runtime errors in my program.
>
> I know that some CMake commands are "inherited" so that
> if it appears in a CMakeLists.txt anywhere in the path
> between the project root and your current directory, that
> command executes; others execute only if they appear in
> CMakeLists.txt in the current directory. Unsurprisingly
> (and quite properly, thinking about how it would most
> typically be used), ADD_DEFINITIONS seems to be "inherited."
>
> My first request is for the CMake documentation to indicate
> for each command whether it is inherited by subdirectories
> or not.
>
> The second, more urgent request is, what command(s) should
> I best put in my CMakeLists.txt file(s) so that FOO will
> be defined for the files compiled by foo.dsp and NOT
> defined for the files compiled by bar.dsp?