Last Updated: February 25, 2016
· siuying

Mitigate “duplicate symbol” errors at link time

If your iOS project use external static library, sooner or later you will encounter errors like this:

ld: duplicate symbol _SBJSONErrorDomain in ...

This happens when you use library A and library B, each use the same third party library such as SBJson.

One usual way to fix this is rename the conflicting symbol. e.g. rename one of the SBJson to some like LibASBJson. This however require manual work and is error prone.

Today I learn a simpler alternative: Use the preprocessor to rename the symbols automatically during the build phase.

For each of the duplicated symbols, add "-DSBJSONErrorDomain=LibASBJSONErrorDomain" flags to the ‘Other C Flags’ build setting for the library project. You'll found no more duplicate symbol after this setting!

Note: If both library are external and you have no access, you probably need to revert to nasty hacks such as removing classes from static libraries or objcopy.

2 Responses
Add your response

Hi, thanks for your neat post. I have two comment/questions
1- When I use two libraries with the same method definition, the latest added library gains precedence and I get no duplicate symbol errors. I wonder if my XCode version (4.6) now allows duplicate symbol definitions. Any idea?
2- It seems objcopy doesn't apply to ARM binaries. Do you know if I could rename symbol definitions in an ARM binary?

The reason why I'm asking these questions is because I need to link several libraries with same method definitions (and different implementations). When I link them all, only one implementation is accessible (the last added library overrides)


over 1 year ago ·
  1. I still encountered duplicates method definition lately, so I believe it is not changed in XCode.

  2. Nope. I never tried that, and if possible, I would use cocoapods and opensource library, and try to prevent the issue from happening rather than hack it (it is sadly not always possible tho)

over 1 year ago ·