1

From what I understand, a linker error due to a duplicate symbol means that:

  1. a symbol was defined in more than one source file
  2. resulting in the same symbol in two different object files after compilation
  3. so the linker does not know which of the two symbols he should link to.

While trying to compile a demo project from www.ugfx.io I came across this error:

duplicate symbol _main in:
    .build/obj/GFXLIB/demos/modules/gdisp/basics/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [.build/demotest.elf] Error 1

In contrast to every duplicate symbol error I could find on the web there is just one file listed as part of this error. And the source file belonging to this object file has just one main symbol. You can view it here.

How shall I even understand this error? And what could be its cause?

303
  • 888
  • 1
  • 11
  • 31
  • There are two possibilities - either the same object code is being linked twice or the source code is included two times. – Ketan Mukadam Aug 08 '17 at 11:32
  • Maybe there is a function called ``_main`` and the classic ``int main(...)``. The compiler just thinks, _main differs from main, and later it just appends "_" to the stdcall-functions and _forgets_ to check the duplicate/to change ``_main`` to another symbol. __So just check the file and try to compile it with another compiler and link it again.__ – cmdLP Aug 08 '17 at 12:04
  • Maybe one of [objdump](https://linux.die.net/man/1/objdump) [nm](https://linux.die.net/man/1/nm) or [readelf](https://linux.die.net/man/1/readelf) can be of help. That is, if you are on Linux. – Vroomfondel Aug 10 '17 at 14:39

1 Answers1

2

It happened to me when I included the same source file twice in the compiling command, e.g. :

$gcc -o main main.c becool.c foo.c bar.c becool.c end.c

The repeated becool.c caused a duplicate symbol error.

So, if this is the problem, just review your compiling command and remove such duplicates

ayehia
  • 85
  • 12
  • This is not an answer. – dbush Dec 05 '17 at 20:03
  • clarification please @dbush – ayehia Dec 05 '17 at 20:17
  • 1
    What you stated could be the problem, but there's not enough info from OP to be sure. It's more appropriate as a comment. – dbush Dec 05 '17 at 20:22
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/18164048) – MD XF Dec 06 '17 at 01:52
  • appreciate your help clarifying " provide answers that don't require clarification from the asker" in this case please @MDXF, I'll be glad if you propose something better – ayehia Dec 06 '17 at 16:32