Link error when using lex and yacc output in Visual Studio

故事扮演 提交于 2019-12-11 07:33:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!