Search code examples
c++buildscons

How can I use custom builders that generate .cpp and .h files?


My project is only composed by two files:

text.foo
main.cpp

main.cpp code looks like this:

#include "foo.h"
#include "bar.h"

int main()
{
 ...
}

I have a custom builder that takes as input text.foo and generates as output the following files:

foo.h
bar.h
text.cpp

text.cpp must be compiled to produce text.o, which in turn must be linked together to main.o to produce the final executable. The .h files are a dependency of main.cpp. I need to write a SConstruct file that ensures the following:

  1. .h files must be generated before main.cpp is compiled
  2. when text.foo is changed, the .h files must be regenerated before main.cpp is compiled

I'm playing around with Builder and Emitters but I seem unable to obtain points 1 and 2. Ideally I'd like to have the following line in the SConstruct file:

Program('myProgram', ["main.cpp", "text.foo"])

Solution

  • You probably need a builder that knows how to build text.cpp and so on from text.foo

    It's a little tricky because you generate multiple files from your text.foo with no relationship. However, your emitter for your builder should state it's creating foo.h, bar.h and text.cpp

    Then you merely need

    Program('myprogram', ['main.cpp', 'test.cpp'])
    

    And you're done, as scons will note main.cpp requires foo.h to build, and will find it has a way of generating foo.h, and test.cpp, and will generate them.