Now suppose that our "Hello, World!" program
actually has an #include
line
to include the hello.h
file in the compilation:
#include <hello.h> int main() { printf("Hello, %s!\n", string); }
And, for completeness, the hello.h
file looks like this:
#define string "world"
In this case, we want SCons to recognize that,
if the contents of the hello.h
file change,
the hello
program must be recompiled.
To do this, we need to modify the
SConstruct
file like so:
Program('hello.c', CPPPATH='.')
The $CPPPATH
value
tells SCons to look in the current directory
('.'
)
for any files included by C source files
(.c
or .h
files).
With this assignment in the SConstruct
file:
%scons -Q hello
cc -o hello.o -c -I. hello.c cc -o hello hello.o %scons -Q hello
scons: `hello' is up to date. % [CHANGE THE CONTENTS OF hello.h] %scons -Q hello
cc -o hello.o -c -I. hello.c cc -o hello hello.o
First, notice that SCons
constructed the -I.
argument
from the '.'
in the $CPPPATH
variable
so that the compilation would find the
hello.h
file in the local directory.
Second, realize that SCons knows that the hello
program must be rebuilt
because it scans the contents of
the hello.c
file
for the #include
lines that indicate
another file is being included in the compilation.
SCons records these as
implicit dependencies
of the target file,
Consequently,
when the hello.h
file changes,
SCons realizes that the hello.c
file includes it,
and rebuilds the resulting hello
program
that depends on both the hello.c
and hello.h
files.
Like the $LIBPATH
variable,
the $CPPPATH
variable
may be a list of directories,
or a string separated by
the system-specific path separation character
(':' on POSIX/Linux, ';' on Windows).
Either way, SCons creates the
right command-line options
so that the following example:
Program('hello.c', CPPPATH = ['include', '/home/project/inc'])
Will look like this on POSIX or Linux:
% scons -Q hello
cc -o hello.o -c -Iinclude -I/home/project/inc hello.c
cc -o hello hello.o
And like this on Windows:
C:\>scons -Q hello.exe
cl /Fohello.obj /c hello.c /nologo /Iinclude /I\home\project\inc
link /nologo /OUT:hello.exe hello.obj
embedManifestExeCheck(target, source, env)