Simple Builds

Here's the famous "Hello, World!" program in C:

    int
    main()
    {
        printf("Hello, world!\n");
    }
 

And here's how to build it using SCons. Enter the following into a file named SConstruct:

    Program('hello.c')
 

That's it. Now run the scons command to build the program. On a POSIX-compliant system like Linux or UNIX, you'll see something like:

    % scons
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    cc -c -o hello.o hello.c
    cc -o hello hello.o
    scons: done building targets.
 

On a Windows system with the Microsoft Visual C++ compiler, you'll see something like:

    C:\>scons
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    cl /nologo /c hello.c /Fohello.obj
    link /nologo /OUT:hello.exe hello.obj
    scons: done building targets.
 

First, notice that you only need to specify the name of the source file, and that SCons deduces the names of the object and executable files correctly from the base of the source file name.

Second, notice that the same input SConstruct file, without any changes, generates the correct output file names on both systems: hello.o and hello on POSIX systems, hello.obj and hello.exe on Windows systems. This is a simple example of how SCons makes it extremely easy to write portable software builds.

(Note that we won't provide duplicate side-by-side POSIX and Windows output for all of the examples in this guide; just keep in mind that, unless otherwise specified, any of the examples should work equally well on both types of systems.)

The SConstruct File

If you're used to build systems like Make you've already figured out that the SConstruct file is the SCons equivalent of a Makefile. That is, the SConstruct file is the input file that SCons reads to control the build.

There is, however, an important difference between an SConstruct file and a Makefile: the SConstruct file is actually a Python script. If you're not already familiar with Python, don't worry. This User's Guide will introduce you step-by-step to the relatively small amount of Python you'll need to know to be able to use SCons effectively. And Python is very easy to learn.

One aspect of using Python as the scripting language is that you can put comments in your SConstruct file using Python's commenting convention; that is, everything between a '#' and the end of the line will be ignored:

      # Arrange to build the "hello" program.
      Program('hello.c')    # "hello.c" is the source file.
   

You'll see throughout the remainder of this Guide that being able to use the power of a real scripting language can greatly simplify the solutions to complex requirements of real-world builds.