Search code examples
c++linuxincludesconsrebuild

CPPPATH doesn't seem to work with scons?


while reading the man page of scons, my understanding was, scons doesn't always realize when a header file is changed, cpp source files should also change. I did an experiment, but only to find, whether we have CPPPATH specified or not, seems scons will always detect header file changes and apply rebuild of corresponding source files.

For example, I've got o.c file and headers/ directory containing n.h file:

#include"headers/n.h"
#include<stdio.h>
int main(){
  printf("hello\n");
  return 2;
}

And my scons SConstruct is like this:

Program('o.c')

When I change the content of n.h, scons will rebuild o.c file. This is quite surprising to me. I tried to change SConscript like this:

Program('o.c',CPPPATH='.')

This time, I hope scons will only check header files under ".", but not under ./headers. Still, scons will rebuild o.c

I moved headers/ to another place above "." directory, and changed o.c to include it with absolute path. When I change n.h, still scons will rebuild o.c

My questions:

(1) How does scons scan and determine if header file has changed, does it call gcc front-end or preprocessor to do this? If so, it seems to be duplicated work with compilation, right?

(2) I don't find specifying CPPPATH useful: whether it's specified, scons will scan. Even when I say CPPPATH=".", scons seems to scan other directories.

Why? Is this by design? If yes, then what's the usage of CPPPATH at all?


Solution

  • Again (see Using 'LIBS' in scons 'Program' command failed to find static library, why? and When I change SConstruct file, scons doesn't trigger rebuild?) your assumptions are wrong.

    SCons simply mimics the usage of CPPPATH as in the gcc/g++ compiler. In your example above, even the gcc will find the header n.h without an explicit "-Iheaders" in the command-line. By your explicit

    #include"headers/n.h"
    

    it has all the information it needs (full relative/absolute path to the header). Make that a

    #include "n.h"
    

    and you will see a difference.