Compiling Multiple Source Files

You've just seen how to configure SCons to compile a program from a single source file. It's more common, of course, that you'll need to build a program from many input source files, not just one. To do this, you need to put the source files in a Python list (enclosed in square brackets), like so:

       Program(['main.c', 'file1.c', 'file2.c'])
    

A build of the above example would look like:

       % scons -Q
       cc -o file1.o -c file1.c
       cc -o file2.o -c file2.c
       cc -o main.o -c main.c
       cc -o main main.o file1.o file2.o
    

Notice that SCons deduces the output program name from the first source file specified in the list--that is, because the first source file was prog.c, SCons will name the resulting program prog (or prog.exe on a Windows system). If you want to specify a different program name, then (as we've seen in the previous section) you slide the list of source files over to the right to make room for the output program file name. (SCons puts the output file name to the left of the source file names so that the order mimics that of an assignment statement: "program = source files".) This makes our example:

       Program('program', ['main.c', 'file1.c', 'file2.c'])
    

On Linux, a build of this example would look like:

       % scons -Q
       cc -o file1.o -c file1.c
       cc -o file2.o -c file2.c
       cc -o main.o -c main.c
       cc -o program main.o file1.o file2.o
    

Or on Windows:

       C:\>scons -Q
       cl /nologo /c file1.c /Fofile1.obj
       cl /nologo /c file2.c /Fofile2.obj
       cl /nologo /c main.c /Fomain.obj
       link /nologo /OUT:program.exe main.obj file1.obj file2.obj