问题
I am using this windows version of flex (lex) and bison (yacc) to port my query compiler from linux to Windows.
The output files, lex.yy.c
, y.tab.c
and y.tab.h
are getting generated properly.
However, I am getting the following link error.
Error 29 error LNK2001: unresolved external symbol _yylval G:\Project\lex.yy.obj QC
Error 30 error LNK1120: 1 unresolved externals G:\Project\QC.exe QC
I tried copying the above files generated by the original linux versions of flex and yacc to Visual studio project folder. But that too gives the same link error.
回答1:
This could happen because you're mixing C++ and C files together in one project. So if you're including y.tab.h
into a .cpp
file then make sure it has extern "C" { ... }
around it. Like this:
extern "C"
{
#include "y.tab.h"
}
Update: From your comment I understood that y.tab.c
is y.tab.cc
now. This causes a link time error. This could be fixed by either making both files C++ (or C). Or by changing yylval
declaration in y.tab.h
to
#ifdef __cplusplus
extern "C" YYSTYPE yylval;
#endif
回答2:
This looks to me like you are linking with a library for flex or bison (been a while since I used it so I can't remember exactly what you have to link with) on unix but not doing that on windows. Are you linking with the same libraries on windows?
来源:https://stackoverflow.com/questions/4770442/link-error-when-using-lex-and-yacc-output-in-visual-studio