Yacc和Lex包含混淆 [英] Yacc and Lex inclusion confusion

查看:109
本文介绍了Yacc和Lex包含混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何正确编译带有调用yyparse的Makefile的程序吗?

I am wondering how to correctly compile a program with a Makefile that has calls to yyparse in it?

这就是我要做的:

我有一个Makefile可以编译所有常规文件,并且它们与y.tab.c或lex.yy.c没有连接(我应该拥有它们吗?)

I have a Makefile that compiles all my regular files and they have no connections to y.tab.c or lex.yy.c (Am I supposed to have them?)

我在代码之上执行此操作:

I do this on top of my code:

#include "y.tab.c"
#include "lex.yy.c"
#include "y.tab.h"

这是我尝试制作程序时发生的事情:

This is what happens when I try to make the program:

当我只输入"make"时,它会给我很多警告.下面显示了一些示例.

When I just type in "make", it gives me many warnings. Some of the examples are shown below.

在功能yywrap': /src/parser.y:12: multiple definition of yywrap'中 服务器 :/src/parser.y:12:首先在这里定义 utils.o:

In function yywrap': /src/parser.y:12: multiple definition ofyywrap' server.o :/src/parser.y:12: first defined here utils.o:

在功能yyparse': /src/y.tab.c:1175: multiple definition of yyparse'中 server.o:/src/y.tab.c:1175:首先在此处定义 utils.o

In function yyparse': /src/y.tab.c:1175: multiple definition ofyyparse' server.o:/src/y.tab.c:1175: first defined here utils.o

我遇到许多不同的错误,它们引用了不同的yy _ ***文件.过去,我已经多次调用yyparse成功编译了,但是这次似乎有所不同.似乎很像某个地方的包含问题,但我无法确定它是什么.我所有的头文件也都具有ifndef条件.

I get many different errors referring to different yy_*** files. I have compiled successfully with multiple calls to yyparse in the past, but this time seems to be different. It seems an awful lot like an inclusion problem somewhere but I can't tell what it is. All my header files have ifndef conditions, as well.

感谢您的帮助!

推荐答案

您应该编译 y.tab.clex.yy.c,但不包括它们.仅在需要它们的源文件中包含头文件(.h).

You should be compiling y.tab.c and lex.yy.c, not including them. Only include the header files (.h) in your sources that need them.

然后链接生成的目标文件以生成可执行文件.

Then you link the resulting object files to produce the executable.

project: project.o t.tab.o lex.yy.o foo.o bar.o

y.tab.o: t.tab.c y.tab.h

lex.yy.o: lex.yy.c

# Assuming that you have correct pattern rules setup 
# or that the implicit rules will do...


此外,您可以使用make来确保这些文件相对于生成文件的文件而言是最新的:


Moreover, you can use make to insure that those files are up-to-date relative there generating files:

y.tab.c y.tab.h: project.yacc
        $(YACC) $<

lex.yy.c: project.lex
        $(LEX) $<

或更需要的更复杂的命令行.

or more sophisticated command lines as needed.

这篇关于Yacc和Lex包含混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆