[CMake] Build one target with multiple subdirectories

Alexander Neundorf a.neundorf-work at gmx.net
Mon Jul 2 10:08:12 EDT 2007


On Monday 02 July 2007 08:51, 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} )

You could also just the the variables in the parent CMakeLists.txt to the full 
path:

set(subdir1_DIR "${CMAKE_CURRENT_SOURCE_DIR}/subdir1")
set( subdir1_SOURCES
      ${subdir1_DIR/hello1.cpp
      ${subdir1_DIR/hello2.cpp)

add_library(irgendwas ${subdir1_SOURCES} )


If you use the idea from James Bigler, I would suggest to name the included 
files not CMakeLists.txt, but something different, e.g. SourceFiles.txt or 
something like this. So people won't expect that these are normal cmake files 
which will be processed as the oother CMakeLists.txt

Alex


More information about the CMake mailing list