[CMake] Linking archives in a sibling directory

Raymond Wan r.wan at aist.go.jp
Thu Dec 2 09:13:37 EST 2010


Hi Michael,


On Thu, Dec 2, 2010 at 19:40, Michael Hertling <mhertling at online.de> wrote:
> On 12/01/2010 06:03 PM, Raymond Wan wrote:
>> Ah!  I see.  Then is it recommended that this top-level CMakeLists.txt
>> have just these lines, or should I move the ADD_EXECUTABLE, etc. lines
>> here as well?  Or is this really "up to me" and there isn't a
>> suggested paradigm?
>
> Basically, IMO, a CMakeLists.txt with ADD_EXECUTABLE() et al. should be
> placed in the same directory as the related source files unless there's
> a reason not to do so; this makes for modularity and a well organized
> code base. The above-mentioned directory structure with its top-level
> CMakeLists.txt containing just ADD_SUBDIRECTORY() commands keeps the
> modules main, dir-A and dir-B separate with minimal interconnections.
> In the previous structure, e.g., main/CMakeLists.txt had to know that
> dir-A resides in "../dir-A"; now, such information is concentrated in
> the top-level CMakeLists.txt, and the sole reference of main to dir-A
> is the target "subprogA_ar". Due to CMake's capabilities to track the
> dependencies among targets, it doesn't matter where dir-A is placed
> within the project's source tree. So, in short, don't move the
> ADD_EXECUTABLE() etc. to the top-level CMakeLists.txt.


I see -- I did not realize this point you made about CMake's
dependency tracking abilities.  So, basically the only thing I need to
worry about is the order of the ADD_SUBDIRECTORY ()'s.  As long as I
put

ADD_SUBDIRECTORY (dir-A)

before

ADD_SUBDIRECTORY (main)

then "main" will be built correctly?

But, if I am not mistaken and following what you are saying, I can
only build "main" using the top-level CMakeLists.txt.  The lower
CMakeLists.txt in main/ does not know the location of "dir-A".  The
only way for both to work is to have this in main/CMakeLists.txt:

ADD_SUBDIRECTORY(../dir-A ${CMAKE_CURRENT_BINARY_DIR}/dir-A)

yet this kind of duplication should be an error at worse; unnecessary
repetition at best?


> The top-level directory of non-trivial projects should not contain any
> source files, perhaps a config.h header or the like. Usually, there're
> enough things gathering in a project's root, e.g. READMEs, subdirs for
> documentation, resources or CMake stuff, a CMakeLists.txt file ;) etc.
> The sources, however, should be organized in their own subdirectories,
> so the top-level CMakeLists.txt typically contains ADD_SUBDIRECTORY()
> commands along with configurational stuff like setting up compiler
> flags, processing options or investigating the underlying system,
> but no ADD_EXECUTABLE() or the like.
>
> Of course, there're exceptions and probably strong different opinions.


I see!  Thank you for this!  I'll stick with standard practice for now
and leave the exceptions and stronger opposing opinions to people who
know more about CMake than me.  :-)

Thank you very much for the explanation!

Ray


More information about the CMake mailing list