问题
I am wondering how to correctly compile a program with a Makefile that has calls to yyparse in it?
This is what I do:
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:
When I just type in "make", it gives me many warnings. Some of the examples are shown below.
In function yywrap':
/src/parser.y:12: multiple definition of
yywrap'
server.o
:/src/parser.y:12: first defined here
utils.o:
In function yyparse':
/src/y.tab.c:1175: multiple definition of
yyparse'
server.o:/src/y.tab.c:1175: first defined here
utils.o
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.
Thanks for your help!
回答1:
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...
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.
来源:https://stackoverflow.com/questions/2486106/yacc-and-lex-inclusion-confusion