在Visual Studio中使用lex和yacc输出时出现链接错误 [英] Link error when using lex and yacc output in Visual Studio

查看:523
本文介绍了在Visual Studio中使用lex和yacc输出时出现链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此 Windows版本的flex( lex)和bison(yacc)将我的查询编译器从linux移植到Windows。
输出文件 lex.yy.c y.tab.c y.tab.h 正在生成。
但是,我得到以下链接错误。

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

我试图将由原始linux版本的flex和yacc生成的上述文件复制到Visual studio项目文件夹。

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.

推荐答案

这可能是因为您在一个项目中混合使用C ++和C文件。因此,如果您将 y.tab.h 包含到 .cpp 文件中,请确保它具有 externC{...} 。像这样:

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"
}

更新: c $ c> y.tab.c 现在是 y.tab.cc 。这将导致链接时间错误。这可以通过两个文件C ++(或C)来修复。或者通过将 y.tab.h 中的 yylval 声明更改为

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

这篇关于在Visual Studio中使用lex和yacc输出时出现链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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