8

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

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.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
mapcan
  • 81
  • 1
  • 2

2 Answers2

7

using gcc -o charcount charcount.o -lfl instead of gcc -o charcount -lfl charcount.o may be help.

It's strange that the order of object file and shared library make crucial sense here, but the reversion really works.

RSFalcon7
  • 2,241
  • 6
  • 34
  • 55
zhengchl
  • 341
  • 3
  • 7
6

Since neither flex nor bison creates the main function for you, it must be your own main() in the code that is getting in the way of the library. Basically, do not put main() into a library.

However, it is only fair to note that both the Flex library (-lfl, /usr/lib/libfl.*) and the Yacc library (-ly, /usr/lib/liby.*) do in fact contain a rudimentary main() program. If you use either or both of those libraries, then, you must make sure your own object file with main() is linked before the libraries are scanned.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278