Clang error: Duplicate Symbol - objective-c

I'm getting this error while compiling
ld: duplicate symbol _OBJC_METACLASS_$_ScaleTypeDisclosureSection in
.../Build/Intermediates/A.build/Debug-iphonesimulator/A.build/Objects-normal/i386/ScaleEditorViewController-DDFF198930F36D54.o
and
.../Build/Intermediates/A.build/Debug-iphonesimulator/A.build/Objects-normal/i386/ScaleEditorViewController-DDFF198930F36D54.o
for architecture i386
This seems really weird because it appears that the linker is trying to link the file with itself. I have searched my entire computer and there exist only 1 .h and one .m file with that name. I've cleaned my project several times. I've tried ripping the ScaleTypeDisclosureSection class out of my app entirely, and in that case, it just gives me a duplicate symbol error for a different object in the ScaleEditorViewController file! I've checked for double imports also. I'm lost!
To clarify, there are a few classes defined in the ScaleEditorViewController.h and ScaleEditorViewController.m file. One is the view controller, the others are little helpers classes. Even if I take every class out but the main class, I still get an error.

This problem is now fixed. I'm not exactly sure why. I think it might have something to do with me having imported files (into Xcode) and setting their target to my test target instead of my main target. Deleting all file references and then dragging all files back into my project solved the problem.

Related

Linker error in Xcode 4.4

Upon upgrading from Xcode 4.3 to Xcode 4.4, I started to get the following error on building my iPhone app:
ld: section __objc_const (address=0x0010C720, size=7265990088) would make the output executable exceed available address range for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The memory address and number do not mean anything to me, but they remain consistent across clean and rebuilds. It is not clear to me how to find what they might be referencing.
The code did and still does compile in XCode 4.3.
Does anyone have any idea how I might track down what's causing this error?
I had the very same error. Luckily, I could recall the moment when it appeared, rolled back and compared the revisions. It was a category with static methods on Google Analytics GAI class. I have no idea why it happened, since other categories in the project work just fine, but hope it helps someone too.
You can see the sizes of all symbols (including global variables) in a Link Map File:
In the Build Settings for your target, go to the "Linking" section and set "Write Link Map File" to "Yes".
Build the program. The linker will fail, but the link map file is written.
Locate the link map file. The log output from the linker shows the parameters -map -Xlinker -/path/to/linkmapfile.txt. It is somewhere in the DerivedData folder of your project.
The link map file show addresses (first column) and sizes (second column) for all symbols. For global variables, it shows the name and in which object file they are located.
You should find the address of your error message (0x0010C720 in your example) in the link map file.
You can check the link map file for other symbols having a large size.
So perhaps this helps to narrow down the problem.

build failed with: ld: duplicate symbol _OBJC_CLASS_$_Algebra5FirstViewController

I am getting this error suddenly when running my app via iPhone simulator:
clang: error: linker command failed with exit code 1 (use -v to see
invocation):
ld: duplicate symbol _OBJC_CLASS_$_Algebra5FirstViewController in
.../Algebra5-anwcuftojtxtgkfootneeeqhwztj/Build/Intermediates/Algebra5.build/Debug-iphonesimulator/Algebra5.build/Objects-normal/i386/ExercisesViewController.o
and
.../Algebra5-anwcuftojtxtgkfootneeeqhwztj/Build/Intermediates/Algebra5.build/Debug-iphonesimulator/PSLE
Algebra5.build/Objects-normal/i386/PSLE_Algebra5FirstViewController.o
for architecture i386
What is it all about?
It look like the class Algebra5FirstViewController is compile multiple time.
Can you make sure that the .m and .mm is only included once in your project sources in Xcode? You can also confirm this by checking in the compile log (last icon at the right, next to the breakpoints icon) and see that confirm that it is only compiled once.
Also, if this class is part of a library that you link against and you have a class with the same name, you could have the same error.
Finally, you can try a clean and rebuild, just in case the old object files are still present and there is some junk in the compiled files. Just in case...
EDIT
I also note that the second reference is made in the file for ExercisesViewController. Maybe there is something in this file. Either you #imported the Algebra5FirstViewController file instead of the .h, or the ExercisesViewController has #implementation (Algebra5FirstViewController) instead of #implementation (ExercisesViewController), or there was some junk with this file that will cleaned with a Clean an Rebuild.
That can happen if the same .m file is referenced multiple times in your target's "Compile Sources" section in "Build Phases". Remove duplicate entries and you should be fine.
also had this problem by declaring a const * NSString in the header file (incorrectly) instead of the implementation file (correctly)
I got this issue because I accidentally imported the .m instead of the .h. Hope reading this saves someone with same problem some time.
I had same problem. Got it solved!
If you have imported any files into project then check .m (main) file for same does exists in Targets (Project Name) -> Build Phases -> Compile Sources.
If file does not exists then include it using (+) Add button shown.
Also, if duplicate files exists (if any) then delete it.
Now press cmd+shift+k to clean the project. New Build should not display this error.
I got a similar error when adding files from another project, like #paiego explained. My error was that I didn't check "Add to targets". As explained above, I deleted the references and added the files again but this time checking "Add to targets".
I was getting similar error and I got it resolved this way.
Try to set Build Active Architecture Only to NO for 'Pods' project and
your app's target
In one case, I saw this error when dragging a new class' .h and .m into the project. The only solution I found was to remove the references to these files and then add them back via the project menu.
I met it when import a ViewController.m in TableViewController. Try to delete '#import "ViewController.m"' if it exited. Hope this help!
'linker command failed with exit code 1 (use -v to see invocation)'- I got this error when running a phonegap application on iPhone. I changed Build Active Architecture Only to Yes and it worked fine.
It seems that for m, I dragged the files into the project and after that didn't work, clicked file "add files to project". Both were the incorrect approach. just drag it into the projects folder (in finder) that houses the other .h and .m files.
once i got
18 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1
For me this was due to that i have used "const NSInteger" in my switch case.
What i did is change the const NSInteger to enum, and replace the reference with enum values.
This fixed the error for me.
This happens either, some files are missing in the "Compile Sources" section, or duplicate entries are found for some file(s). In my case, I had duplicate entries for two files, I deleted one entry for each of the files, that solved my problem. Hope this helps.
I found this article that provided a solution for me. It pertains to Xcode 7 where the default for No Common Blocks is Yes rather than No in previous versions.
This is a quote from the article:
The problem seems to be that the "No common blocks" in the "Apple LLVM
6.1 - Code Generation" section in the Build settings pane is set to Yes, in the latest version of Xcode.
This caused what I will describe as circular references where a class that was included in my Compile Sources was referenced via a #import in another source file (appDelegate.m). This caused duplicate blocks for variables that were declared in the original base class.
Changing the value to No immediately enabled my app to compile and resolved my problem.
I'm posting a new answer to this because I ran into this error and had to use a different solution that I think is specific to iOS 9.
I had to explicitly disable the Enable Bitcode in Build Settings, which is automatically turned on in the update.
Referenced answer: New warnings in iOS 9
[XCODE 7.1 UPDATE]
First Option That Worked:
I changed the Deployment Target from 7.1 to 8.1 and the error went away.
Hope that helps someone.
UPDATE (day 2): Second time I'm back here in 2 days.
On day 2 I started to get more errors in addition to this annoying error. The problem was a conflicting file that Xcode didn't recognize. I used the links below as help:
"_OBJC_CLASS_$_viewsampleViewController", referenced from:
‘ld: warning: directory not found for option’
And ultimately:
"_OBJC_CLASS_$_viewsampleViewController", referenced from:
I was deleting everything from the FrameWork Search Paths + Compile Resources + Run Script Phases + the pods and then re-installing them again to ultimately find success.
The issue on my size (on day 2) was more of a problem with conflicting files or files that Xcode didn't recognize.
But ultimately, removing everything (mentioned above and in the links), cleaning a lot of times and reinstalling the pods/frameworks/run script phases helped.
Really hope this helps someone.
Running Xcode 4.6.3 on OSX 10.7.5 was able to fix this error by changing Architectures to Native Architecture of Build Machine from $(NATIVE_ARCH_ACTUAL).
I received this once by (accidentally) importing both the .h and .m files into the same class.
I got this error while implementing a subclass without the necessary framework added (MPMoviePlayerController without the MediaPlayer framework, in this example)
I got this same issue when working with Bolts.framework. It turns out that when Bolts gets corrupted, no amount of re-importing will fix the issue. I had to go back to the original download file and reimport it completely from scratch. This fixed it right away. It was such a weird issue that it took me forever to figure it out... hopefully this tip will spare some of you from the same frustrations :)
This occurred for me when I named a UILabel reference and an int the same thing, I didn't get an error when I typed it only when I tried to run it so I didn't realize that that was the problem, but if you have something like a label which is the "score" and you call it score, and name an int which is the score also score then this problem occurs.
Variations on this issue can occur if you have a missing library or file. Check there aren't any missing files in your Project Explorer: they'll be written in red if Xcode can't find them.
"Link Binary With Libraries" had old project name pod library. Fixed after removal.
List item
Project
Build Phases
Link Binary With Libraries
Remove deprecated library.
I had this same issue with a library, and I tried all of the answers listed here and nothing helped.
I ended up simply removing the library from Link Binary With Libraries and then re-adding it and it worked fine.
The only (and unfailing) way to resolve this issue is building test from command line:
xcodebuild -workspace MyProject.xcworkspace/ -scheme MyScheme -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 7,OS=10.3.1' test
So, at this point, your compilation will surely fail but you'll see all linking problem. In my case, I had several problem such as:
ld: framework 'Foo' not found
To resolve this, you need to on your target, BuildSettings->Linking->OtherLinkerFlags and remove 'Foo' framework .
Use of unresolved identifier 'ClassName' .
To resolve this, I need to add/check file's target membership to UITest target too.
Other possible problems will be raised by xcodebuild and you can easily fix it.
Simply cleaning CMD + SHIFT + K, then building CMD + B worked for me.
In my case I had a reference to a library in Other Linker Flags. Removing it got rid of the error.
I had the same error.
I had imported a audio frame work that i was no longer using. I removed it (DO NOT DELETE IT!) and it built successfully.

Strange error when compiling iPad application(ld: duplicate symbol _OBJC_CLASS_$)

when i compile my project, i have this error :
ld: duplicate symbol _OBJC_CLASS_$_DNCloseButton in /Users/.../Library/Developer/Xcode/DerivedData/...Objects-normal/i386/DNCloseButton-7045D069F03DAA13.o for architecture i386
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang++ failed with exit code 1
I know that the problem is about the class DNCloseButton but i don't how to solve it ? thanks for your answers.
Ensure you've only declared the interface once, in a header file (.h), and imported that header file with #import and not #include.
Sometimes XCode cocks up a little bit and links the same file twice at the linker stage. To fi this:
Remove the file from your project (select the 'remove references' option) and then add it again.
Clean with shift-cmd-k
Build
Hope this helps!
This will happen if a source file is include twice in your Compile Sources Build Phase.
To check this in Xcode 4:
Select your Project from the file list.
Select your target from the left column.
Click on the Build Phases tab on the top bar.
In the search field, type in the name of the class that is causing the linker error. In the case of the question above, it is DNCloseButton (ignore the preceding _).
If the source file for the class appears twice in the Compile Sources list, delete one of the entries.
You might also get this error if you by accident #import the .m file instead of the .h file.
This may happen in the following cases.
You have put the same class implementation into two different files;
You actually have just one implementation of this class, however you are also linking in your project a framework or library containing a class whose name is exactly the same of yours.
Try finding in the whole project your class and make sure only one copy is available within your project.

Removing a method call from inside a static lib(.a) without recompiling

I'm using a static lib thats giving me a warning when uploading my binary for review by apple.
The method in the static lib that causes the warning(non-public selectors) is never called by me, its corresponding .h is deleted from my proj, but warning still persists.
Given that I know the method name causing the problem, is there a way for me to open/edit this .a and comment/delete the offending piece of code and then use the modified .a in my project.
I don't have access to the .a source to recompile it, and its very old and the creator of it has no contact details for me to track down.
Many Thanks,
-Cake
Quick and dirty solution: Open the .a file in a hex editor and change all instances of the name. Leave the function name the same length so that offsets in the file don't change, just change a letter or something like that. I did a quick test, adding a dummy function to a subproject we're building as a static library then tweaking the function name in the .a file (there were five instances, for what that's worth) and everything built okay. I don't see any reason it wouldn't pass the App Store check after that.
I'm really surprised the function was still there in the final build, though—I thought Dead Code Stripping was supposed to clean out any unused code. Huh.
http://opensource.apple.com/source/cctools/cctools-809/
I don't presume to get your bounty, because I haven't provided an easy solution. But yes, it in theory is possible. You have your work cut out for you.
There are several solutions, depending on your lib and project.
In your build settings :
Enable "dead code stripping" if possible : If the method is never used (even internally), the symbol will be deleted.
Use "Unexported symbol file" : Simply add the symbol into a file and it will be removed from the binary. This will work even if the symbol is used internally.
Enable "Deployment Postprocessing" and "Strip Linked Product" with "Strip Style" set to "All symbol"
(Not sure) Use "Symbols Hidden by Default". This is related to the code generation and should not affect linking, but just in case everything above failed...
No need to hack the binary files. Just turn off the compiler's "unused selectors" warning: -fno-unused-selectors.

duplicate symbol _OBJC_METACLASS_$_ClassName

I am trying to run my cocos2d application and facing this error, as I have recently upgraded my sdk to 4.2 and my cocos2d to 0.99.5.
I tried to clean my project even change the Target ios Deployment but error remains the same. Also rename the className aswell in order to avoid duplication from Library classes names of cocos2d.
ld: duplicate symbol _OBJC_METACLASS_$_MenuSceneNew in
/Users/umaidsaleem/Desktop/functionalityTest/build/Debug-iphonesimulator/libcocos2d
libraries.a(MenuScene.o) and
/Users/umaidsaleem/Desktop/functionalityTest/build/functionalityTest.build/Debug-iphonesimulator/functionalityTest.build/Objects-normal/i386/MenuScene.o
Another rather simple thing to check is that you did not accidentally #import a .m file instead of a .h header file.
Happened to me.
You are linking the file MenuScene.m twice. You apparently have it in a static library and also in your main application.
Another reason for this, for those out there who did the same as I just did, might be that you re-created a managed object class. By doing that and choosing another group in the project structure, you accidentally create another reference to the same files.
I ended up with two references for header and implementation in both the root of the project and in my model group. Removing the topmost references to .h and .m files got rid of the linking failure.
Sometimes Core Data inserts AnyProjectNameModel.xcdatamodeld to the Compile Sources of the Build Phases. Check it and delete it and everything is run again...
The error message just indicates the generated ManagedObjects...
The correct place for the AnyProjectNameModel.xcdatamodeld is the Copy Bundle resources in the Build Phases.
Check the Compile Sources of your target and see if a class.m file is included twice
A stupid mistake that I made was that I named my #implementation the same as my superclass.
In the .h
#interface Subclass : Superclass
#end
In the .m
#import "Subclass.h"
#implementation Superclass
#end
None of the regular suggestions from google seemed to help, so if someone is as crazy or tired as I am (maybe a healthy mix of both), make sure you haven't duplicated an #implementation!
In my case this was happening because of Xcode automatically generating my NSManagedObject class, which then clashed with my manually created NSManagedObject class. In this case you can set "Codegen" to "Manual/None" in the Data Model Inspector for your newly created Entity.
Another thing to check for the double linking - its possible to have the same file in two different places in the file list on the left. Then the compiler compiles and links it twice.
This happens when playing with organizing your file hierarchy, for example.
You don't have to be making a library or anything fancy - just drag a .m in to two different locations in the project tree.
Remember to only remove the reference to one of them, no file trashing is needed.
To me, removing -all_load from 'Other Linker Flags' fixed the problem.
I have also had this particular error occur when a file that has not been added to the project, is referenced somewhere in the project. The two cases where I have experienced this are where I have deleted/removed files from the project without removing references to them, and when working cooperatively on a project where a teammate added the reference but I have not added the file to my version of the project.
I know the OP has resolved their problem, but I felt this might help someone else who reads this question while looking for help.
I have figured out the solution myself, sorry for posting little delayed. All assistance are much appreciated the but problem arises when I add new CCLayer derived class and checkmark cocos2d 0.99.5 static library which was wrong. Then I re-Ad my class without checking and clean my targets and then build and go. Problem solved now.
I had a project with 2 targets. A target named Component and the second named Sample. Sample was using component as a link library.
Then Component was using a pod named MyPod (installed with cocoapod).
The Podfile was written like this :
def shared_pods
pod 'MyPod'
end
target 'Component' do
shared_pods
end
target 'Sample' do
shared_pods
end
Both targets was referencing the shared pod list. When building Component target, no problem appears, but when building sample i get a duplicate symbol _OBJC_METACLASS_$_ClassName.
I have changed the Podfile to :
def shared_pods
# empty
end
target 'Component' do
pod 'MyPod'
shared_pods
end
target 'Sample' do
shared_pods
end
And that solve the problem.
3 hours spent, hope to save somebody time.
XCode Beta crashed for me while deleting a reference to a class. This caused the problem described in the answer, the fix was different again.
In my Target's Build Phase, under "Compile Sources", the item giving me grief was red. I couldn't remove it with the minus button, but typing "skip" in the compiler flags caused the red class to disappear after reloading the project. I'm assuming you could type anything in there.
It took me two hours to find the solution to my prob, I put it in here for another option to those having this problem, although it is probably not going to be a common one.
None of the Answers worked for me, so what I did was remove the files from the project and choose only Remove References.
Then I added back the same files and it worked.
That fixed my error. Maybe XCode got lost references stored somewhere since the project is multi-collaboration.
Hope that helps someone.
Another possibility for error like this is when there is a class name collision. Either 2 classes with the same name within your project, or class from your project clashing with one defined in some of dependency libraries.
Remove target from right panel and again add target. This will remove any duplicate references.
It may happen if 2 programmer add the same target and commit the project file.
In my case I was using a binary library includes the same class name I was using outside,
So I changed my class name and It's working fine :)
In my case, I had added the libxxxx.a to Link Binary with Libraries section in the subProjects as well as in the mainProject. Hence there were duplicate symbols. Removing from subProjects fixed it.