Chapter 2. Simple Builds

The single most important thing you do when writing a build system for your project is to describe the "what": what you want to build, and which files you want to build it from. And, in fact, simpler builds may need no more. In this chapter, you will see several examples of very simple build configurations using SCons, which will demonstrate how easy SCons makes it to build programs on different types of systems.

2.1. Building Simple C / C++ Programs

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

#include <stdio.h>

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

And here's how to build it using SCons. Save the code above into hello.c, and enter the following into a file named SConstruct:

Program('hello.c')
      

This minimal build file gives SCons three key pieces of information: what you want to build (a program); what you want to call that program (its base name will be hello), and the source file you want it built from (the hello.c file). Program is a Builder, an SCons function that you use to instruct SCons about the "what" of your build.

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 -o hello.o -c 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 /Fohello.obj /c hello.c /nologo
link /nologo /OUT:hello.exe hello.obj
embedManifestExeCheck(target, source, env)
scons: done building targets.

Notice that SCons deduced quite a bit here: it figured out the name of the program to build, including operating system specific suffixes (hello or hello.exe), based off the basename of the source file; it knows an intermediate object file should be built (hello.o or hello.obj); and it knows how to build those things using the compiler that is appropriate on the system you're using. It was not necessary to instruct SCons about any of those details. This is an example of how SCons makes it easy to write portable software builds.

For the programming languages SCons already knows about, it will mostly just figure it out. Here's the "Hello, World!" example in Fortran:

program hello
  print *, 'Hello, World!'
end program hello
   
Program('hello', 'hello.f90')
   
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gfortran -o hello.o -c hello.f90
gfortran -o hello hello.o
scons: done building targets.