[CMake] Subdirs

Brad King brad.king at kitware.com
Tue Mar 1 11:09:38 EST 2005


Michael Seydl wrote:
> how can i add files in subdirs to the a library, app or whatever in a 
> parent directory ( both directories have CMakeLists.txt)
> Additionally i'd like to place these files into a seperate source_group.
> 
> I'd like to know if its possible to generate a project with subdirs and 
> place these subdirs into a project with source_groups according to the 
> subdirs they're in.

The simple way is to not have the CMakeLists.txt file in the 
subdirectory.  You can specify a relative path to the source files in 
the parent directory:

SET(A_SRCS A/a1.cxx A/a2.cxx)
SOURCE_GROUP(A FILES ${A_SRCS})

SET(B_SRCS B/b1.cxx B/b2.cxx)
SOURCE_GROUP(B FILES ${B_SRCS})

ADD_EXECUTABLE(myexe ${A_SRCS} ${B_SRCS})

If you want to still have a listfile in each subdirectory containing the 
sources specific to that directory, you can put each SET/SOURCE_GROUP 
pair into a file in that directory:

# A/local.cmake
SET(A_SRCS A/a1.cxx A/a2.cxx)
SOURCE_GROUP(A FILES ${A_SRCS})

# CMakeLists.txt
INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/A/local.cmake)
INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/B/local.cmake)

-Brad


More information about the CMake mailing list