How can I eliminate the β€œcore” flex & bison routine so that I can put the vocabulary and parsing process into a library?

I am working on a parser that parses a json string, and I want to make it a library. The problem is that when I use ld to link the library I wrote, an error message appears:

main.o: In function `main': main.c:(.text+0x0): multiple definition of `main' json-parser.o:/build/buildd/flex-2.5.35/libmain.c:29: first defined here 

how can i fix this? thanks.

+6
parsing flex-lexer lex bison yacc
source share
2 answers

using gcc -o charcount charcount.o -lfl instead of gcc -o charcount -lfl charcount.o can help.

It is strange that the order of the object file and the shared library is crucial here, but the reversal really works.

+6
source share

Since neither flex nor bison creates the main function for you, it should be your own main() in the code that interferes with the library. Basically, do not put main() in the library.

However, it is fair to say that both the Flex library ( -lfl , /usr/lib/libfl.* ) and the Yacc library ( -ly , /usr/lib/liby.* ) do contain a rudimentary main() program. If you use one or both libraries, you must make sure that your own object file is linked to main() before scanning the libraries.

+5
source share

All Articles