Link C Flex lexer to C++ parser -
i using flex develop lexer language of mine. want create parser in c++ , using own approach it. since lexer in c (flex), want compile lexer (gcc -c lex.yy.c -lfl
) , program (g++ -c file1.cc file2.cc ...
) separately , link them create final executable.
in particular, after creating lexer, write following code c++ program:
#include <iostream> extern int yylex(); int main(int argc, char** argv); int main (int argc, char** argv) { yylex(); return 0; }
but link error when link stuff like:
g++ main.cc lex.yy.o -lfl
but get:
/tmp/ccbrhobp.o: in function `main': main.cc:(.text+0x10): undefined reference `yylex()' collect2: ld returned 1 exit status
where problem? thankyou
what happens here compile c code, this:
int yylex() { // ... }
it generates yylex
symbol. compile c++ code. c++ allows 2 different functions same name exist, can't use name of function symbol name. when write int yylex();
on c++ code, gcc looks symbol: _z5yylexv
. doesn't find, link error. solution c function , should use name symbol name:
#include <iostream> extern "c" int yylex(); int main (int argc, char** argv) { std::cout << yylex() << std::endl; return 0; }
Comments
Post a Comment