简体   繁体   中英

How to exclude certain #include directives from C++ stream?

I have this C++ file (let's call it main.cpp ):

#include <string>
#include "main.y.c"
void f(const std::string& s) {
  yy_switch_to_buffer(yy_scan_string(s.c_str()));
  yyparse();
}

The file depends on main.yc , which has to be generated beforehand by means of bison util. In other words, I can't compile main.c file if I forget to run bison main.y before it. And it's perfectly OK, this is how I want it. Now I'm trying to build .d file from Makefile , using this command:

$ c++ -MM main.c > main.d
main.cpp:2:10: error: main.y.c: No such file or directory

I fail here, since main.yc is not ready yet. I think that I should somehow quote my #include directive in the main.c file to make it invisible for c++ -MM process.

This sounds like a job for a makefile. You can set the dependencies such that main.c depends on main.yc , and main.yc has a rule to build it from the bison code.

You can indicate in your makefile that main.c depends on main.yc so that it'll run the bison process before it tries to compile main.c .

As an alternative (which I think is probably not what you want to do) is that you can have your makefile pass a macro to the compiler to indicate whether or not main.yc exists and use an #if directive to include (or not) main.yc .

#if EXISTS_MAIN_Y_C
#include "main.y.c"
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM