Reputation: 559
I have a C++ program that processes an input file. I want to add pre-processing ability to my program. That is say the input file looks like :
%pre-processing section
#include <some_parent_file>
#define x y
#ifdef 0
some useless text
#endif
%actual file-contents
... lots of text ...
Then my program should automatically include the text from parent file, do the #define stuff and other pre-processing. I could use a script (with g++ -E) before calling my program but I would like to be able to do this within my program as that allows more flexibility.
Also "g++ -E" will assume a pre-processor directive when lines in the "actual file-contents" section start with a hash (g++ -E doesn't know that I want to separate my code into 2 sections!).
Moreover, if I can use just the ifdef functionality within "actual file contents" section, that would be amazing.
Can I embed C++ code within my program to use only the features I want from pre-processing capability of gcc compiler ?
Upvotes: 0
Views: 253
Reputation: 2441
If you want to turn on/off features at compile time, it is best to stick to standard preprocessor macros or use templates. You can also look at how Qt parses the standard C++ code to generate additional code. At runtime, you can achieve this with scripting or maybe a plug-in system.
Upvotes: -1
Reputation: 1
A simple possibility would be to use popen
to read from a command pipe, which could be cpp
(or gcc -C -E
) or m4
.
A related possibility is to embed a scripting interpreter in your program, e.g. lua. A related solution is to make your application embedded in an interpreter like Python or Ocaml.
At last, you could use ordinary lexing and parsing techniques, perhaps with ANTLR and process yourself your include directives. You can also use library for configuration files, like libconfig
Upvotes: 1