[CMake] When should I use add_subdirectory and when ExternalProject?

Theodore Papadopoulo Theodore.Papadopoulo at inria.fr
Mon Feb 4 09:29:35 EST 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/04/2013 09:23 AM, Ansis Māliņš wrote:
> If I have a dependency (e.g. SDL2) that seems to work with 
> add_subdirectory just fine, should I still use ExternalProject_Add 
> instead? Given both ways work, what should I prefer? What are the
> tradeoffs?

This is just my view on this topic.
Personnally, I prefer using add_subdirectory even though
ExternalProject_Add is probably cleaner (no possible interaction
between variables of different subdirs).

Why do I prefer the add_subdirectory, well mostly because:

 1) ExternalProject_Add and add_subdirectory do not play very well
together. External_Project_Add does all its work at make time, so that
all UseXXX et al are not generated at configure time. So if you use a
add_subdirectory for a project with a dependency that its build with
ExternalProject_Add that will not work.

2) When you use External_Project_add with a download, and get a
compilation error, you tend to correct some sources that are placed in
the build directory which can be very easily removed... This probably
can be corrected with some External_Project_add magic, but I do not
know if you can strictly separate the build from the source stuffs.

3) That being said, it is true that External_Project_add is a little
bit cleaner and should work directly with subprojects that have a
functionning build system (not necessarily cmake by the way which is
another advantage). Using add_subdirectory requires to avoid
conflicting variables and often some changes from
CMAKE_{SOURCE,BINARY}_DIR to CMAKE_CURRENT_{SOURCE,BINARY}_DIR.
The same thing can be said for cmake macros (which one do you take), ...

Still, when add_subdirectory works, it is in my view simpler and better.

	Theo.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.13 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlEPxc8ACgkQEr8WrU8nPV2CdACffDVj8hkJKsRnWzZJOiir+ef/
IIEAn1bzlrfggpkdd9+3eYffwB+rxFlu
=eb+k
-----END PGP SIGNATURE-----


More information about the CMake mailing list