Cross-Platform Builds via Scons

1 minute read

If you want cross-platform C/C++ builds, there are basically only two real options: CMake and Scons. CMake is faster and more widely used, but appears to be kind of a pain and requires learning a special scripting language. In constrast, Scons is a bit less polished, but is simple and allows one to write build files in pure Python. I recently had to get a ~10kloc project building on multiple platforms and so, knowing Python and not needing anything fancy, I decided to try Scons.

Unfortunately, while the documentation makes clear how to do simple builds wherein everything is in the same directory and/or has no dependencies to other directories, it’s not clear how to do a hierarchical build with a realistic project structure.

To save anyone else out there the pain of figuring out how to do this, here’s an example hierarchical build via Scons. The basic idea is that there’s a root “Sconstruct” file that kicks off the whole build by calling “sconscript” files in the roots of the source and test directories.

This system will work regardless of your source and test directory structures, since it recursively adds all source directories to the build path. You could also recursively call custom “sconscript” files within each directory if you wanted to ensure that there weren’t bizarre dependencies between modules, but for a small-ish project, the include-everything-ever solution is convenient.

2017 Edit: Scons has caused me nothing but pain. Use Bazel. You can find an example in the C++ code for Bolt.