[CMake] Re: Sharing code between projects

Matthew Woehlke mw_triad at users.sourceforge.net
Tue Aug 28 11:40:47 EDT 2007


kitts wrote:
> Anyway, I have a new set of projects and my current problem is that i have 
> some code that i share between projects. Once source can be shared not 
> binaries.
> 
> Code is organized as;
> src/common
> src/project1
> src/project2
> 
> The CMakeLists.tst files are located inside for each project which sets the 
> right compiler and sources to be built. Now i want common to be included in 
> each of these proejects and build separately for each by inheriting all the 
> settings for that project.
> 
> What is the best way to achieve this? I cant use ADD_SUBDIRECTORY() as it is 
> really a sibling folder (cmake complains if I use "../common").

The way I did this was to use the somewhat dubious solution:

IF(PROJECT STREQUALS FOO)
   ADD_DIRECTORY(foo)
ELSEIF(PROJECT STREQUAL BAR)
   ADD_DIRECTORY(bar)
ELSE(PROJECT STREQUALS FOO)
   MESSAGE(FATAL_ERROR "You didn't tell me what PROJECT to build!")
ENDIF(PROJECT STREQUALS FOO)

...and then, obviously, add e.g. '-DPROJECT:STRING=FOO' to your initial 
cmake run.

My source tree looks like:

build-foo/
build-bar/
src/
   common/
   foo/
   bar/

-- 
Matthew
"What, more work?" -- Peasants (Warcraft II)



More information about the CMake mailing list