[CMake] SUBDIRS command

Alexander Neundorf a.neundorf-work at gmx.net
Tue Nov 20 18:53:53 EST 2007


On Wednesday 21 November 2007, Jesse Corrington wrote:
> I have a project that has many different os targets that use different sets
> of source files and it builds a single exe. The source tree is like this
> commonSource/morespecific/morespecific. I figured I would have the common
> CMakeLists create a list of common files and call subdirs on the correct
> specific dirs for the other source. These specific dirs would then add to
> the source list the parent created. However the source list variable
> doesn't seem to keep it's value between multiple files. What is the best
> way to handle this. Thanks

The values are not kept because of the way how variables "inherit" their 
values in cmake. In subdirs they start with the value from the parent dir, 
but modifications don't get distributed back to the parent dir. That's why 
you can add as many source files to a variable in a subdir, they will 
not "arrive" in the parent dir.

You could either include() the files in the subdirs or you could build static 
libraries in the subdirs and link the application to these static libs.

If you chose to use include(), beware that if you have e.g. set(SRCS 
special.c) in basic/specific/morespecific/CMakeLists.txt and include that 
file in basic/CMakeLists.txt, special.c is then expected to be in 
basic/special.c. So you have then to use the relative path to the file:
set(SRCS specific/morespecific/special.c).

You can automate that a bit by using something like the following 

get_filenamecomponent(dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(SRCS ${dir}/special.c)

Additionally, if you use this style with include()ing the other files, I would 
recommend to name the included files not CMakeLists.txt but something else, 
e.g. TargetSources.txt or something.

Hope that helps
Alex


More information about the CMake mailing list