开发者

How to use cpp (c pre-processor) with include path

开发者 https://www.devze.com 2023-02-17 16:22 出处:网络
I am writing a parser which operates on C/C++ source files. As 1st stage of parser I need to use som开发者_如何学Ce already available preprocessor, so that I can get away with directives such as #defi

I am writing a parser which operates on C/C++ source files. As 1st stage of parser I need to use som开发者_如何学Ce already available preprocessor, so that I can get away with directives such as #define, #ifdef. For g++, the available tool is 'cpp' (or g++ -E ...). I had 2 questions:

  1. My parser is written in C++, so what could be the best way to run this 'cpp' on other C/C++ source files? Is it something like:

    system("cpp sourceFile.cpp parsed_sourceFile.cpp"); // just a pseudo code
    

    Or is there any better way?

    (I want to know only the possible ways by which I can perform a preprocessing on given files. Are these commands like system(), or popen() are standard way to do such thing?)

  2. How can I take care of the non-standard included files inside the source files? I will not know of anything that at which place these user defined header files are stored.


system is the most cross-platform way possible. But if your code is single-platform, you can have better control over the execution of subprograms by using platform's native API. Like ShellExecute in windows.

There are a variety of places non-standard include files can be located, such as in the same directory as the source script. Such directories are usually mentioned in the project and MAK file of the projects. You can open those files and look for extra include locations mentioned in them. But the easier ;) way is of course to ask the user himself where those include files are.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号