Getting Started with SCons

Woman using a computer in an office

GrapchicStock / Getty Images

SCons is a next-generation make utility that is much easier to configure and use than make. Many developers find make syntax not just difficult to get into but quite ugly. Once you've learned it, it's okay, but it has a bit of a steepish learning curve.

So that's why SCons was devised; it's a better make and considerably easier to use. It even tries to figure out what compiler is needed and then supplies the right parameters. If you program in C or C++ on Linux or Windows then you should definitely check SCons.

Installation

To install SCons you need to have Python already installed. If you are using Linux then most likely you'll have Python already. If you have Windows you can check if you already have it; some packages might have installed it already. First, get a command line. Click the start button, (on XP click Run), then type cmd and from the command line type python -V. It should say something like Python 2.7.2. Any version 2.4 or higher is ok for SCons.

If you haven't got Python then you need to download and install 2.7.2. Currently, SCons does not support Python 3 so 2.7.2 is the latest (and final) 2 version and the best one to use. However, that may change in the future so check the SCons requirements.

Follow the instructions for installing SCons. It's not complicated; however, when you run the installer, if it's under Vista/Windows 7 make sure you run the scons.win32.exe as administrator. You do this by browsing to the file in Windows Explorer and right click then Run As Administrator.

Once it's installed then, assuming you have any of Microsoft Visual C++ (Express is ok), MinGW tool chain, Intel Compiler or the PharLap ETS compiler already installed, SCons should be able to find and use your compiler.

Using SCons

As a first example, save the code below out as HelloWorld.c.

int main(int arcg,char * argv[])
{
printf("Hello, world!\n");
}

Then create a file called SConstruct in the same location and edit it so it has this line below in it. If you save the HelloWorld.c with a different filename, make sure that the name inside the quotes matches.

Program('HelloWorld.c')

Now type scons at the command line (in the same place as HelloWorld.c and SConstruct) and you should see this:

C:\cplus\blog>scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
cl /FoHelloWorld.obj /c HelloWorld.c /nologo
HelloWorld.c
link /nologo /OUT:HelloWorld.exe HelloWorld.obj
scons: done building targets.

This built a HelloWorld.exe which when run produces the expected output:

 C:\cplus\blog>HelloWorld
Hello, world!

Notes

The online documentation is very good for getting you started. You can refer to the terse single file man (manual) or the friendlier more verbose SCons Users Guide.

SCons makes it easy to remove unwanted files from the compilation just add the -c or -clean parameter.

scons -c

This gets rid of HelloWorld.obj and the HelloWorld.exe file.

SCons is cross-platform, and while this article has been about getting started on Windows, SCons comes prepackaged for Red Hat(RPM) or Debian systems. If you have another flavor of Linux, then the SCons guide gives instructions for building SCons on any system. It's open source at its best.

SCons SConstruct files are Python scripts so if you know Python, then you'll have no probs. But even if you don't, you only need to learn a small amount of Python to get the best out of it. Two things you should remember, though:

  1. Comments start with #
  2. You can add print messages with print("Some Text")

Note that SCons is only for non-.NET, so it can't build .NET code unless you learn SCons a bit more and create a specific builder.