Often it is useful to give your program some information from the BuildGen configuration. It can be something as simple as putting the project name and version number so that you have them in one place. There are also some things that are very difficult to do without custom header files such as recording install locations and per-system configurations.
In this tutorial we will build upon the project from the main tutorial ([UserDocs_Tutorial]). You can download our starting setup here.
First we are going to modify "printer/printer.c" so that it will respect --version
. The modified program goes as follows.
#include <stdio.h> #include <string.h> #include "info.h" // Our header file that defines int main ( int argc, char **argv ) { if ( strcmp(argv[1], "--version") == 0 ) { printf("%s version %s.\n", PROGRAM_NAME, PROGRAM_VERSION); return 0; } int i; for ( i = 1; i < argc; i++ ) printf("%s ", argv[i]); putchar('\n'); return 0; }
Fairly simple. If the first argument is "--version" the program prints it's name and version info.
All the info is gained from the header file "info.h" But what is this file? It doesn't exist yet. It will be generated on build. To generate the file we will use S.c.generateHeader()
it takes 2 arguments. The first argument is the name of the file to be generated. The second argument is a table where the keys are the variable names and the values are the values of the variables.
Add the following to "printer/Buildinfo":
S.c.generateHeader("@info.h", { PROGRAM_NAME = projectName, PROGRAM_VERSION = version })
Fairly simple, generates "@info.h" (will be "/src/build/printer/info.h") which defines PROGRAM_NAME
and PROGRAM_VERSION
.
Now run BuildGen and build the project. You probably get an error that looks something like this:
/data/Desktop/UserDocs_HowTos_GeneratedHeaderFile/printer/printer.c:4:18: fatal error: info.h: No such file or directory compilation terminated.
The file exist, but the compiler can't find it. That is easily remedied.
S.c.addInclude "@" -- Include the generated header
This includes the build directory. This is a pretty common practice. Just make sure that this line comes before S.c.compile()
or else the include won't be set for that compile.
Now your program should build with no problem (remember to re-run BuildGen after changing the file).
You setup should now look something like this.
If you run printer --version
you should get a nice message. That is all there is to it.
Anonymous