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.)

Cleaning Up After a Build

When using SCons, it is unnecessary to add special commands or target names to clean up after a build. Instead, you simply use the -c or --clean option when you invoke SCons, and SCons removes the appropriate built files. So if we build our example above and then invoke scons -c afterwards, the output on POSIX looks 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.
      % scons -c
      scons: Reading SConscript files ...
      scons: done reading SConscript files.
      scons: Cleaning targets ...
      Removed hello.o
      Removed hello
      scons: done cleaning targets.
   

And the output on Windows looks 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.
      C:\>scons -c
      scons: Reading SConscript files ...
      scons: done reading SConscript files.
      scons: Cleaning targets ...
      Removed hello.obj
      Removed hello.exe
      scons: done cleaning targets.
   

Notice that SCons changes its output to tell you that it is Cleaning targets ... and done cleaning targets.