Skip to content
Published On:
Mar 14, 2018
Last Updated:
Mar 14, 2018

SCons is a build system.

The logo for the SCons build system.

Installation

Ubuntu 16.04

You can install scons with:

Terminal window
$ sudo apt install scons

However…

The default scons provided by 16.04 (xenial) is very out-of-date (v2.4.1). You can get the latest scons (v3.0.1) as of Mar 2018) by first adding the following line to /etc/apt/sources.list:

Terminal window
deb http://cz.archive.ubuntu.com/ubuntu bionic main universe

And then install scons with the command:

Terminal window
$ sudo apt install scons

Basic Hello, World Example

Prerequisites

  • scons is installed on your system

Steps

  1. Create a new folder called “HelloProject”. All following files in this example will be created in this folder.

  2. Create a file called main.cpp with the following content:

    #include <iostream>
    int main() {
    std::cout << "Hello, world." << std::endl;
    return 0;
    }
  3. Create a file called sconstruct (no file extension) with the following content:

    env = Environment() hello = Program([“hello.cpp”])

  4. From the console, run scons in the current folder:

    Terminal window
    $ scons
  5. This should start the build process and produce an executable called hello. Run the executable with the following command:

    Terminal window
    $ ./hello
    "Hello, world."
  6. Finished!

Importing And Exporting

scons variables may be shared between sconscript files through importing, _exporting _and returning.

Before you can import a variable, it must be exported. You can do this by calling Export() and providing it with a space-separated string of all the variables you wish to export:

Export(‘env installDir’)

You can then import this from a different Sconscript file:

Import(‘env installDir’)

You can return a single object from a Sconscript with the Return() function:

Return(‘myLib’)

The can be captured in the calling Sconscript file with:

myLib = Sconscript(‘src/mySubSconscript’)

Building

By default, scons does not perform an out-of-source build. You can perform an out-of-source build by including the following line in your root-level sconstruct file:

SConscript("src/MyApp.scons", variant_dir="build", duplicate=0)

where src/MyApp.scons points to a child scons file.