[CMake] ADD_SUBDIRECTORY() with a higher level directory

Daniel Dekkers d.dekkers at cthrough.nl
Sun Nov 6 10:27:58 EST 2011


Every project's code base should be organized in the way that suits the
needs best, whatever the needs may be. ;-) E.g., my personal liking for the
general directory hierarchy of a project xyz - library or not - is

xyz
  xyz
  examples
  utilities
  ...

i.e., the project's main part is organized as a sibling of typical
supplementary parts like examples and utilities, and perhaps, one likes to
centralize the tests in another sibling xyz/tests, too.
This approach makes it easier to cope with a growing complexity; e.g., you
do not need to worry where to put the examples if the main part is about to
be split in, say, xyz-{core,gui,net,...}.
In other words: Separate the main part(s) of a project from its
supplementary parts and make the code base reflect this clearly.
Again, that's just my personal taste, and others certainly vary.


Ok...
The top level directory/bundle name would probably have a version number:

xyz-3.1.4
CMakeLists.txt
+ xyz <- represents the lib
  CMakeLists.txt
  + src
    CMakeLists.txt
  + rsrc
+ examples
  CMakeLists.txt
  + example0 
    CMakeLists.txt
    + src
      CMakeLists.txt
    + rsrc
...
+ tests
+ utilities
...

With... 

xyz-3.1.4/CMakeLists.txt
Main entry point for complete build, PROJECT(all), ADD_SUBDIRECTORY(xyz),
ADD_SUBDIRECTORY(examples)

xyz-3.1.4/xyz/CMakeLists.txt
Setup context for the lib

xyz-3.1.4/xyz/src/CMakeLists.txt
Add the sources/resources of the lib, PROJECT(xyz), ADD_LIBRARY(...)

xyz-3.1.4/examples/CMakeLists.txt
Enumerate the examples, PROJECT(examples), ADD_SUBDIRECTORY(example0),
ADD_SUBDIRECTORY(example1),...

xyz-3.1.4/examples/example0/CMakeLists.txt
Setup context for example0

xyz-3.1.4/examples/example0/src/CMakeLists.txt
adds the sources/resources of example0, PROJECT(example0),
ADD_EXECUTABLE(...), ADD_DEPENDENCY(xyz)

...

This "template" stuff should really be in the documentation, it would have
helped me a lot.
More than http://www.cmake.org/cmake/help/examples.html. A library called
"Hello", really?

Thanks,
Daniel



More information about the CMake mailing list