yegor256
yegor256

Reputation: 105083

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.y.c, 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.y.c 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.

Upvotes: 1

Views: 1247

Answers (2)

Mark B
Mark B

Reputation: 96251

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

Upvotes: 3

Michael Burr
Michael Burr

Reputation: 340218

You can indicate in your makefile that main.c depends on main.y.c 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.y.c exists and use an #if directive to include (or not) main.y.c.

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

Upvotes: 1

Related Questions