1

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.

softwarematter
  • 28,015
  • 64
  • 169
  • 263

2 Answers2

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
detunized
  • 15,059
  • 3
  • 48
  • 64
  • y.tab.h is included in the autogenerated file lex.yy.c. In fact the include section is specified in the input file to the lex Lexer.l. – softwarematter Jan 22 '11 at 21:03
  • 1
    Where is `yylval` defined in your project? Usually it's in `y.tab.c`. Could that be that one of your .c files is compiled as C++? – detunized Jan 22 '11 at 23:22
  • In my linux makefile that works, `y.tab.c` is compiled as `g++ -c y.tab.c`. When I moved it to Visual Studio, I got 100 odd cmath errors that went away only when I renamed it to `y.tab.cc` to force it to be compiled as a C++ file. Now my `YYSTYPE yylval;` definition is in `y.tab.cc`. Could that be a problem? – softwarematter Jan 22 '11 at 23:30
  • Just to be clear, I haven't renamed `lex.yy.c` since in the original linux make file, it is compiled as `gcc -c lex.yy.c`. – softwarematter Jan 22 '11 at 23:32
  • 1
    Yes, that is a problem. The `yylval` variable is defined in a C++ file now and cannot be visible from a C file anymore. Simple solution would be to make all files C++. I don't know if they would compile just like that though. – detunized Jan 22 '11 at 23:36
  • When I change everything to C++, the yylval error went away but some items that are defined in lex.y.cc as `#ifndef __cplusplus extern int isatty (int ); #endif /* __cplusplus */ ` gives `identifier not found` errors. How to deal with these. – softwarematter Jan 22 '11 at 23:42
  • 1
    Try to remove `#ifndef __cplusplus`. And you might want to use `_isatty` on Windows instead. MSDN says `isatty` is deprecated. – detunized Jan 22 '11 at 23:44
  • Getting this one now. `error LNK2019: unresolved external symbol int __cdecl isatty(int)" (?isatty@@YAHH@Z) referenced in function "void __cdecl yy_init_buffer(struct yy_buffer_state *,struct _iobuf *)" (?yy_init_buffer@@YAXPAUyy_buffer_state@@PAU_iobuf@@@Z)' – softwarematter Jan 22 '11 at 23:52
  • 1
    Then remove the `isatty` prototype altogether and just `#include ` instead. I think that should do it. – detunized Jan 23 '11 at 00:01
0

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?

jcoder
  • 29,554
  • 19
  • 87
  • 130