Xcode ‘duplicate symbol’ error while building with static library
Question: I have an Xcode 4.1 project that has a static library attached to it. That library handles my Twitter methods and it includes another static library, namely libOAuth.a. On building the app I keep getting “duplicate symbol” error. I build and run my app on 3.2 or 4.1 simulator without errors. When I build the very the same code for a device – for an iPhone 4GS or an iPad – I am getting the errors. I am sure nothing is duplicated. It seams that there are two different architectures (armv6, armv7) in the library and the Xcode can’t resolve them correctly. What is your experience?
Answer: Theoretically there is nothing wrong with linking static libraries together. Xcode is smart enough to ignore two instances of the same file. If you are getting this error try to find a project configuration error first. When you select to build for device, Xcode may not find the proper values, such as “Architectures” is not set – for example – to “Optimized (armv7)” for your iPhone 4GS or iPad. Your code is expected to be built without errors in the Standard architecture as well.
After all these compliments to Xcode designers, I have to say that do not attempt to build static libraries into other libraries. It would be a perfect fit for duplicate symbol errors and it leads to a total mess sooner than later. The best practice is to add your static libraries to your applications only.
Rebuild your application and duplicate symbol error messages will likely disappear.
When you get ‘Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 failed with exit code 1’ message you may have a misconfigured project. Click on your target and and select Get Info (cmd+i). Look at the Architectures group and check out values. Compare them to a working project setting.