Duplicate symbol build error in Xcode project - cocoa

When I try to build my project I get the following error.
ld: duplicate symbol
.objc_class_name_GLFunView in
/Users/gin/Documents/development/GLFun/build/GLFun.build/Debug-iphonesimulator/GLFun.build/Objects-normal/i386/GLFunView-7A51E8797CBB3D72.o
and
/Users/gin/Documents/development/GLFun/build/GLFun.build/Debug-iphonesimulator/GLFun.build/Objects-normal/i386/GLFunView-7A51E8797CBB3D72.o
What is this error about? How can I track down where the error is? XCode usually highlights the code that has problems, but for this error it's not showing anything? It doesn't have anything to do with Interface Builder does it?
My research indicates that this might be caused by including something twice, but I don't understand how that's possible since I'm not using any #include statements, I'm only using #import statements.
Here's some more of the build output:
Ld
build/Debug-iphonesimulator/GLFun.app/GLFun
normal i386 cd
/Users/gin/Documents/development/GLFun
setenv MACOSX_DEPLOYMENT_TARGET 10.5
setenv PATH
"/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2
-arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk
-L/Users/gin/Documents/development/GLFun/build/Debug-iphonesimulator
-F/Users/gin/Documents/development/GLFun/build/Debug-iphonesimulator
-filelist /Users/gin/Documents/development/GLFun/build/GLFun.build/Debug-iphonesimulator/GLFun.build/Objects-normal/i386/GLFun.LinkFileList
-mmacosx-version-min=10.5 -framework Foundation -framework UIKit -framework
CoreGraphics -framework OpenGLES
-framework QuartzCore -o /Users/gin/Documents/development/GLFun/build/Debug-iphonesimulator/GLFun.app/GLFun

I ran into the same problem today. It turned out to be a typo in an #import statement. I accidentally included the .m file instead of the header:
#include "MyClass.m"
instead of:
#include "MyClass.h"

My guess is you're #implementing GLFunView twice in the same file (GLFunView.m). Perhaps you meant to implement GLFunView and then implement a category on it, and forgot the category name?

Related

Xcode 6.2 OSX Undefined symbols for functions in other files

I'm sure this is a total nube question but if someone could explain to me what's going wrong I'd be very grateful.
I create a new Cocoa App in XCode. Call it LinkerTest. This basic app will build and run, putting a simple blank window up.
Add a new .cpp file using the C++ File template. Call it Test.cpp. This creates Test.h too.
In Test.cpp add a simple function:
int TestMe(void)
{
return 1;
}
Declare my function in Test.h
int TestMe(void);
In my AppDelegate.m (which was created automatically when I create the app) add
#include "Test.h"
In the applicationDidFinishLaunching method add:
printf("Test = %d\n", TestMe());
Now try to build. Everything compiles ok, but it fails to link. This is the linker command:
Ld Build/Products/Debug/LinkerTest.app/Contents/MacOS/LinkerTest normal x86_64
cd /Users/chip/LinkerTest
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/chip/LinkerTest/Build/Products/Debug -F/Users/chip/LinkerTest/Build/Products/Debug -filelist /Users/chip/LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64/LinkerTest.LinkFileList -Xlinker -rpath -Xlinker #executable_path/../Frameworks -mmacosx-version-min=10.10 -stdlib=libc++ -fobjc-arc -fobjc-link-runtime -Xlinker -dependency_info -Xlinker /Users/chip/LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64/LinkerTest_dependency_info.dat -o /Users/chip/LinkerTest/Build/Products/Debug/LinkerTest.app/Contents/MacOS/LinkerTest
And this is the error I get:
Undefined symbols for architecture x86_64:
"_TestMe", referenced from:
-[AppDelegate applicationDidFinishLaunching:] in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So any idea what I've done wrong? Seems to me that adding the file it should link too, but it doesn't. I know the file Test.cpp gets compiled, if I add garbage to the file, then the compiler immediately yacks on that garbage.
EDIT: A couple more things.
1) Looking at this stackoverflow(Getting XCode to include, compile and link existing (C++) codebase in XCode 4.3(.1)) question it seems similar but not my issue. I can confirm that my test.cpp is listed in my Compile Sources under Build Phases.
2) Looking in the LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64 folder I find Test.o, Test.d, and Test.dia which suggests to me that Test.cpp is in fact compiling. In that folder I also find LinkerTest.LinkFileList which when opened with a text editor shows that Test.o should be linked.
The answer is that when you create a default Cocoa app the AppDelegate file is a .m file. Changing that file to be AppDelegate.mm fixes the problem. My uninformed guess is that a .m file will only handle .c files and to correctly handle .cpp files you must use .mm files. Why xcode's default files for a default Cocoa App wouldn't be .mm files is beyond my pay grade, but there it is.

Swift linking errors in trivial OS X Command Line App

I have been using a simple command line app from within Xcode (not directly via swiftc) to test code that otherwise gives Playground fits. After several months, and compiler updates, I cannot link seem to link a trivial and contrived snippet that extends a Swift Array. I have code like this in an iOS app, and it compiles just fine. I must have missed the memo at some point, but I am not sure what that was.
Here is a simple snippet (in a main.swift file) that:
protocol MyProtocol : SequenceType {
func fubar()
}
extension Array : MyProtocol {
func fubar() {}
}
println("Hello, World!")
That causes a multitude of linking errors like:
Ld /Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Products/Debug/Hacks normal x86_64
cd /Users/Redacted/development/tests/Hacks
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Products/Debug -F/Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Products/Debug -filelist /Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Intermediates/Hacks.build/Debug/Hacks.build/Objects-normal/x86_64/Hacks.LinkFileList -mmacosx-version-min=10.10 -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx -Xlinker -force_load_swift_libs -lswiftRuntime -lc++ -framework Foundation -lcurses -Xlinker -add_ast_path -Xlinker /Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Intermediates/Hacks.build/Debug/Hacks.build/Objects-normal/x86_64/Hacks.swiftmodule -Xlinker -dependency_info -Xlinker /Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Intermediates/Hacks.build/Debug/Hacks.build/Objects-normal/x86_64/Hacks_dependency_info.dat -o /Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Products/Debug/Hacks
duplicate symbol __TTWSaSs12SequenceTypeFS_8generateUS__USs13GeneratorType___fRQPS_FT_QS1_9Generator in:
/Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Intermediates/Hacks.build/Debug/Hacks.build/Objects-normal/x86_64/main.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx/libswiftCore.a(Swift.o)
duplicate symbol __TTWSaSs12SequenceTypeFS_oi2tgUS__USs13GeneratorType___fMQPS_FTS1_TVSs19_UnderestimateCountT___Si in:
/Users/Redacted/Library/Developer/Xcode/DerivedData/Hacks-Redacted/Build/Intermediates/Hacks.build/Debug/Hacks.build/Objects-normal/x86_64/main.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx/libswiftCore.a(Swift.o)
... <>
Could there be some new linking requirements that I missed? Seems like it must be something simple, but it is escaping me. For all it's worth, I have duplicated this on a separate machine and am using Xcode 6.1.1 (6A2008a) - latest as of the time of posting.
Edited to clarify that I am attempting to build this with Xcode, not from the command line.

Unit testing Objective-C++ frameworks with Xcode 4.x

I'm writing a framework using Objective C++ and I am having trouble setting up unit tests for it.
Compiling just the framework target works fine.
But when I tell Xcode to compile and run the test bundle I get:
Ld ~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug/TestFrameworkTests.octest/Contents/MacOS/TestFrameworkTests normal x86_64
cd "~/Projects/TestFramework"
setenv MACOSX_DEPLOYMENT_TARGET 10.7
"/Applications/Xcode 4.5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++" -arch x86_64 -bundle -isysroot "/Applications/Xcode 4.5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk" -L~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib -F~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug "-F/Applications/Xcode 4.5.app/Contents/Developer/Library/Frameworks" -filelist ~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Intermediates/TestFramework.build/Debug/TestFrameworkTests.build/Objects-normal/x86_64/TestFrameworkTests.LinkFileList -mmacosx-version-min=10.7 -v -fobjc-arc -fobjc-link-runtime -fprofile-arcs -stdlib=libc++ -framework SenTestingKit -framework Cocoa -framework TestFramework -o ~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug/TestFrameworkTests.octest/Contents/MacOS/TestFrameworkTests
Apple clang version 4.0 (tags/Apple/clang-421.10.48) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.0
Thread model: posix
"/Applications/Xcode 4.5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -bundle -macosx_version_min 10.7.0 -syslibroot "/Applications/Xcode 4.5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk" -o ~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug/TestFrameworkTests.octest/Contents/MacOS/TestFrameworkTests -L~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib -filelist ~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Intermediates/TestFramework.build/Debug/TestFrameworkTests.build/Objects-normal/x86_64/TestFrameworkTests.LinkFileList -framework SenTestingKit -framework Cocoa -framework TestFramework -force_load "/Applications/Xcode 4.5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a" -framework Foundation -lobjc -lc++ "/Applications/Xcode 4.5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.profile_osx.a" -lSystem "/Applications/Xcode 4.5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.osx.a" -F~/Library/Developer/Xcode/DerivedData/TestFramework-axdefcbatoubjbbfqiyxildilobl/Build/Products/Debug "-F/Applications/Xcode 4.5.app/Contents/Developer/Library/Frameworks"
Undefined symbols for architecture x86_64:
"Foo::Bar::Bar()", referenced from:
-[FooBar_Tests testBaz] in FooBar_Tests.o
"Foo::Bar::baz() const", referenced from:
-[FooBar_Tests testBaz] in FooBar_Tests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have:
C++ Language Dialect: c++11
C++ Standard Library: libc++
set on both build targets (framework & test bundle).
And I am of course linking against the framework in my test bundle target.
Furthermore, all framework headers are marked public.
I also tried adding the framework files to the test bundle's target and leaving them out. None of these fixed the problem.
I am a bit puzzled as to what's going wrong here, right now. Any ideas?
This is how my C++ class looks like (kind of):
//FooBar.hh
namespace Foo {
class Bar {
public:
bool baz() const;
}
}
//FooBar.mm
#import "FooBar.hh"
namespace Foo {
bool Bar::baz() const {
return true;
}
}
And this my test case:
//FooBar_Tests.hh
#import <SenTestingKit/SenTestingKit.h>
#interface FooBar_Tests : SenTestCase
#end
//FooBar_Tests.mm
#import "FooBar_Tests.hh"
#import <TestFramework/FooBar.hh>
//this one fails as well (compiles fine, fails one linkage):
//#import "FooBar.hh"
#implementation FooBar_Tests
- (void)testBaz {
Foo::Bar bar();
STAssertEquals(bar.baz(), true, nil);
}
Edit: Split code up into .hh&.mm files. Still getting the same errors though.
One possible problem is that your unit tests are in the header file. Header files do not do much in OCUnit. You can even get rid of the header file and put all your test case code in the implementation file. Add an Objective-C unit test class to your project, give it the extension .mm, and move your test case code there. Does that fix the problem?
When I unit test C++ code with OCUnit, I find I have to add the C++ files in my app to the unit test target to avoid link errors. I'm not sure if it applies to Objective-C++ code, but it's something to look into.
I eventually added a new unit test target to my project, enabled C++11 and tried compiling/running it. Success.
Somehow my original unit test target must have gone bad in regards to C++11. Had compiled just fine before.
Now it's time to migrate my test cases to the new test bundle, I guess.
…and I thought I was going mad. Oh well…

Duplicate symbol from single library in iOS4.1 SDK

I am developing an iPhone application. I am unfamiliar with Xcode, so please bear with me. I have the iOS 4.1 Device SDK. When I select "Simulator" in the "Active ..." drop-down box, my application compiles without errors and runs in the iPhone simulator.
When I select "Device" in the drop-down box, however, I get the following linker error regarding a duplicate symbol:
Ld build/PineCone.build/Debug-iphoneos/PineCone.build/Objects-normal/armv6/PineCone normal armv6
cd /Users/isaacsutherland/fydp/PineCone/PineCone
setenv IPHONEOS_DEPLOYMENT_TARGET 4.1
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.1.sdk -L/Users/isaacsutherland/fydp/PineCone/PineCone/build/Debug-iphoneos -L/Users/isaacsutherland/fydp/PineCone/PineCone/../3rd/libGHUnitIPhone -F/Users/isaacsutherland/fydp/PineCone/PineCone/build/Debug-iphoneos -filelist /Users/isaacsutherland/fydp/PineCone/PineCone/build/PineCone.build/Debug-iphoneos/PineCone.build/Objects-normal/armv6/PineCone.LinkFileList -dead_strip -all_load -ObjC -miphoneos-version-min=4.1 -framework Foundation -framework UIKit -framework CoreGraphics /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20.a /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20Core.a /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20Network.a /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20Style.a /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20UI.a /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20UICommon.a /Users/isaacsutherland/fydp/PineCone/3rd/three20/Build/Products/Debug-iphoneos/libThree20UINavigator.a -framework QuartzCore -framework CFNetwork -framework MobileCoreServices -framework SystemConfiguration -lz.1.2.3 /Users/isaacsutherland/fydp/PineCone/ClientDal/build/Debug-iphoneos/libClientDal.a -lGHUnitIPhone4_0 -o /Users/isaacsutherland/fydp/PineCone/PineCone/build/PineCone.build/Debug-iphoneos/PineCone.build/Objects-normal/armv6/PineCone
ld: duplicate symbol _RedirectionLimit in /Users/isaacsutherland/fydp/PineCone/ClientDal/build/Debug-iphoneos/libClientDal.a(libASIHTTPRequest.a-armv6-master.o) and /Users/isaacsutherland/fydp/PineCone/ClientDal/build/Debug-iphoneos/libClientDal.a(libASIHTTPRequest.a-armv6-master.o)
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
The error is strange because it complains that _RedirectionLimit is found twice -- in the same file! libClientDal.a(libASIHTTPRequest.a-armv6-master.o) is the offending library. Can someone help me understand what is going on? How could this library have compiled properly in the first place? Or perhaps the linker is trying to include the same library twice?
The workaround provided in this similar question does not work for me.
If you need more information, I will gladly provide it -- as I said, I'm new to Xcode development.
When you have a web of projects that compile to static libraries and refer to one another, there are 2 distinct concerns you must consider:
A project's Direct Dependencies inform Xcode which projects depend on each other so it knows to recompile a project when its dependencies change.
A project's Linked Libaries actually get included in its object code.
In a nutshell, your web of direct dependencies can be as tangled as you want, but you must be careful to link each project's code into the application executable only once.
Basically, my problem was that I had 3 projects A, B, and C, and the dependencies looked like A=>B, A=>C, B=>C. I was linking libC.a into both A and B, so the linker complained about duplicate code.
The configuration stuff you need to change is on the Target Info page for each of your projects' targets.
This happened to me when I used the -all_load linker flag, which forces the linker to load all symbols from all libraries. The Three20 project says you should use it, because otherwise the Categories won't be loaded and you get a runtime exception. I removed that flag, and added the -force_load flag just for each library that needed it (the Three20 libraries). See also: What does the -all_load linker flag do?
I've several apps that need to embed a small custom library that makes use of ASI and TBXML. Some of this apps have their own version of that libraries. In order to avoid duplicate symbol issue, I duplicated the each library's target removing the .m files that causes the issue.
Hope it helps.

How can my XCode project files be exactly the same as a working project and still have build errors?

I'm following a tutorial from chapter 12 of the book "Beginning iPhone 3 Development." When I try to build the project I get an error. The book came with the project files, so I went through every file in my Classes directory and copied and pasted the code from the example project so that they are identical. This has solved my build errors in the past. I checked that the same frameworks were installed and the files in the Other Sources and Resources folder are identical. Still, when I try to build, I get the following build error:
Ld
build/Debug-iphonesimulator/GLFun.app/GLFun
normal i386 cd
/Users/gin/Documents/development/GLFun
setenv MACOSX_DEPLOYMENT_TARGET 10.5
setenv PATH
"/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2
-arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk
-L/Users/gin/Documents/development/GLFun/build/Debug-iphonesimulator
-F/Users/gin/Documents/development/GLFun/build/Debug-iphonesimulator
-filelist /Users/gin/Documents/development/GLFun/build/GLFun.build/Debug-iphonesimulator/GLFun.build/Objects-normal/i386/GLFun.LinkFileList
-mmacosx-version-min=10.5 -framework Foundation -framework UIKit -framework
CoreGraphics -framework OpenGLES
-framework QuartzCore -o /Users/gin/Documents/development/GLFun/build/Debug-iphonesimulator/GLFun.app/GLFun
ld: duplicate symbol
.objc_class_name_GLFunView in
/Users/gin/Documents/development/GLFun/build/GLFun.build/Debug-iphonesimulator/GLFun.build/Objects-normal/i386/GLFunView-7A51E8797CBB3D72.o
and
/Users/gin/Documents/development/GLFun/build/GLFun.build/Debug-iphonesimulator/GLFun.build/Objects-normal/i386/GLFunView-7A51E8797CBB3D72.o
From what I can see my project is identical to the one supplied with the book, which I can build and run successfully. What could possibly still be different that is causing this error?
Use diff(1) between the two projects' project.pbxproj files and find out.
Clean the project then try again, sometimes strange things happen. Check the build configurations between the two projects to see if they have any differences (building for the same architecture, sdk, etc.). Since you have duplicate symbols my first guess would be that Clean will solve your issue.

Resources