winflexbison icon indicating copy to clipboard operation
winflexbison copied to clipboard

%option c++ is not compatible with %option noyywrap - linker error: multiple definition

Open lexxmark opened this issue 5 years ago • 5 comments

related to https://github.com/westes/flex/issues/472

lexxmark avatar Dec 25 '20 00:12 lexxmark

@ledaniel2 I don't see implementation of the yyFlexLexer::yywrap in the header file https://github.com/lexxmark/winflexbison/blob/8063e9b9b6d0aa330e3137e0d66470116d906f7f/flex/src/FlexLexer.h#L145

Why you get a link error?

How to reproduce your issue?

lexxmark avatar Jan 06 '21 04:01 lexxmark

The definition is in the generated header and implementation file, admittedly the problem goes away if FlexLexer.h is used instead of the generated header (which I hadn't realized before), but in the more complex project I have this is not an option.

lexer_bug.l:

%option c++ noyywrap outfile="ScannerX.cpp" header="ScannerX.hpp"

%%

.	return 0;

main.c:

#include "ScannerX.hpp"

int main() {
    FlexLexer *lex = new yyFlexLexer;
    lex->yylex();
}
>win_flex --wincompat lexer_bug.l
>cl /EHsc /I. /Fetest.exe main.cpp ScannerX.cpp
[...]
Generating Code...
Microsoft (R) Incremental Linker Version 14.28.29334.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
main.obj
ScannerX.obj
ScannerX.obj : error LNK2005: "public: virtual int __thiscall yyFlexLexer::yywrap(void)" (?yywrap@yyFlexLexer@@UAEHXZ) already defined in main.obj
test.exe : fatal error LNK1169: one or more multiply defined symbols found

Both ScannerX.hpp and ScannerX.cpp contain: int yyFlexLexer::yywrap() { return 1; }

So to clarify, it's all of %option c++ noyywrap and header= which appear to be incompatible when using #include with the generated header. The offending line in winflexbison is flex\src\main.c:1774

ghost avatar Jan 06 '21 11:01 ghost

I've added inline code to flex\src\main.c:1774 and now there is no link error.

The correct fix could be done in the upstream project.

lexxmark avatar Jan 08 '21 05:01 lexxmark

@ledaniel2 please try this package https://ci.appveyor.com/api/buildjobs/5cibc2240rafovcs/artifacts/build%2Fwin_flex_bison-dev-2019-Win32-Release.zip

lexxmark avatar Jan 08 '21 06:01 lexxmark

@lexxmark Thanks for that, I can confirm that it works with the test program here, also with my other project.

I'm happy for this issue to be closed here, however what a more correct fix for upstream would be I don't know. I'll continue to watch.

ghost avatar Jan 08 '21 13:01 ghost