Search code examples
objective-cgcclinker-errors

GCC complaining about a duplicate definition (where only one exists)


I just seem to hit one error after another when dealing with larger projects in Xcode. This time, GCC is complaining of a duplicate definition for a struct-printing function - one where it is actually defined (in a different file), and one where it is used (in the driver).

The specific error I'm getting is this:

ld: duplicate symbol _fprintConfiguration in
/.../bits.build/Objects-normal/x86_64/Block.o and
/.../bits.build/Objects-normal/x86_64/bits.o

where Block.o contains the definition and bits.o contains the driver.


I've done some research on this already, but it seems that every problem exists with people actually defining it twice by virtue of #include, but in all my files I only use #import. Isn't the #import directive supposed to 'intelligently' include files? Are there any other reasons I could be getting this error? Are there any other solutions I can try?

Thanks for your help :)


Solution

  • The problem is that you're including the same definition in multiple translation units (both Block.o and bits.o). To fix this, declare the function inline or move the definition to a (single) source (non-header) file.

    This is orthogonal to the question of #include vs. #import. That's about including the same code multiple times in the same translation unit.