Building and Linking with Libraries

One of the more useful ways in which you can use multiple construction environments is to link programs with different sets of libraries.

Building Libraries

You build your own libraries by specifying Library instead of Program:

      env = Environment()
      env.Library('foo', ['f1.c', 'f2.c', 'f3.c'])
    

SCons uses the appropriate library prefix and suffix for your system. So on POSIX or Linux systems, the above example would build as follows (although ranlib may not be called on all systems):

      % scons
      cc -c f1.c -o f1.o
      cc -c f2.c -o f2.o
      cc -c f3.c -o f3.o
      ar r libfoo.a f1.o f2.o f3.o
      ranlib libfoo.a
    

On a Windows system, a build of the above example would look like:

      C:\>scons
      cl /Fof1.obj f1.c
      cl /Fof2.obj f2.c
      cl /Fof3.obj f3.c
      lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
    

The rules for the target name of the library are similar to those for programs: if you don't explicitly specify a target library name, SCons will deduce one from the name of the first source file specified, and SCons will add an appropriate file prefix and suffix if you leave them off.