[CMake] Build one target with multiple subdirectories
James Bigler
bigler at cs.utah.edu
Mon Jul 2 09:08:51 EDT 2007
Hi,
The problem with using SUBDIR is that variables will get scoped to the
subdirectory and not automatically available in the parent. There are
two ways around this.
The method I use heralds from CMake 1.8. You simply include the
CMakeLists.txt files directly, thus putting everything in the same scope:
INCLUDE(subdir1/CMakeLists.txt)
INCLUDE(subdir2/CMakeLists.txt)
ADD_LIBRARY(irgendwas
hello.cpp
${subdir_1_SRCS}
${subdir_2_SRCS}
)
In subdir1/CMakeLists.txt
SET(subdir_1_SRCS
subdir1/hello1.cpp
subdir1/hello2.cpp
)
In subdir2/CMakeLists.txt
SET(subdir_2_SRCS
subdir2/hello3.cpp
subdir2/hello4.cpp
)
The other option is to get the value using GET_DIRECTORY_PROPERTY (note
the following code may or may not work, because I don't have a concrete
example of it):
GET_DIRECTORY_PROPERTY(subdir_1_SOURCES DIRECTORY subdir1
DEFINITION subdir_1_SOURCES)
This looks like an example of using GET_DIRECTORY_PROPERTY:
http://public.kitware.com/pipermail/cmake-commits/2006-October/000303.html
James
Michael Hammer wrote:
> Hi!
>
> I would like to add a library and the sources are distributed in
> different directories, something like the following example:
>
> / CmakeLists.txt
> hello.cpp
> subdir1 / hello1.cpp
> hello2.cpp
> CMakeLists.txt
> subdir2 / hello3.cpp
> hello4.cpp
> CMakeLists.txt
>
> In the CMakeLists.txt of subdir1 I would write:
>
> set( subdir1_SOURCES
> hello1.cpp
> hello2.cpp )
>
> similar in subdir2
>
> set( subdir2_SOURCES
> hello3.cpp
> hello4.cpp )
>
> in /CMakeLists.txt I would add something like that:
>
> add_subdirectory(subdir1)
> add_subdirectory(subdir2)
>
> add_library( irgendwas
> hello.cpp
> subdir_1/${subdir1_SOURCES}
> subdir_2/${subdir2_SOURCES} )
>
> Unfortunately this doesn't work ;) CMake doesn't set the variables
> subdirx_SOURCES by calling add_subdirectory. The next problem I see is
> the add_library statement. In subdir1_SOURCES there is a necessary path
> prefix (subdir1/, subdir2/) missing - I am really not sure if I can add
> the prefix the way I have done it here (obviously not ;)).
>
> Perhaps someone can help? I am sure there is a "good" or "accurate" way
> to do this - thanks a lot.
>
> Michael
>
More information about the CMake
mailing list