简体   繁体   English

如何从C ++流中排除某些#include指令?

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

I have this C++ file (let's call it main.cpp ): 我有这个C ++文件(我们称之为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. 该文件依赖于main.yc ,它必须事先通过bison util生成。 In other words, I can't compile main.c file if I forget to run bison main.y before it. 换句话说,如果我忘记在它之前运行bison main.y ,我就无法编译main.c文件。 And it's perfectly OK, this is how I want it. 这完全没问题,这就是我想要的。 Now I'm trying to build .d file from Makefile , using this command: 现在我正在尝试使用以下命令从Makefile构建.d文件:

$ 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. 我失败了,因为main.yc尚未准备好。 I think that I should somehow quote my #include directive in the main.c file to make it invisible for c++ -MM process. 我认为我应该以某种方式在main.c文件中引用我的#include指令,使其对c++ -MM进程不可见。

This sounds like a job for a makefile. 这听起来像是一个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. 您可以设置依赖关系,以便main.c依赖于main.yc ,而main.yc有一个规则来从bison代码构建它。

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 . 您可以在makefile中指出main.c依赖于main.yc以便在尝试编译main.c之前运行bison进程。

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 . 作为替代方案(我认为可能不是你想要做的)是你可以让你的makefile将一个宏传递给编译器来指示main.yc是否存在并使用#if指令来包含(或不包含) main.yc

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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