如何从 C++ 中排除某些 #include 指令溪流?
我有这个 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();
}
该文件依赖于 main.yc
,它必须预先通过 bison< 生成/代码> 实用程序。换句话说,如果我忘记在 main.c 文件之前运行
bison main.y
,我就无法编译它。完全没问题,这就是我想要的。现在我尝试使用以下命令从 Makefile
构建 .d
文件:
$ c++ -MM main.c > main.d
main.cpp:2:10: error: main.y.c: No such file or directory
我在这里失败了,因为 main.yc
尚未准备好。我认为我应该以某种方式在 main.c
文件中引用我的 #include
指令,以使其对 c++ -MM
进程不可见。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像是 makefile 的工作。您可以设置依赖项,使
main.c
依赖于main.yc
,并且main.yc
有一条从 bison 构建它的规则代码。This sounds like a job for a makefile. You can set the dependencies such that
main.c
depends onmain.y.c
, andmain.y.c
has a rule to build it from the bison code.您可以在 makefile 中指出
main.c
依赖于main.yc
,以便它在尝试编译之前运行bison
进程main.c
。作为替代方案(我认为这可能不是您想要做的)是您可以让 makefile 将宏传递给编译器以指示
main.yc
是否存在并使用#if
指令包含(或不包含)main.yc
。You can indicate in your makefile that
main.c
depends onmain.y.c
so that it'll run thebison
process before it tries to compilemain.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
.