Swift linking errors in trivial OS X Command Line App - xcode

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.

Related

OSX application fails to compile after adding the Firebase Library

I'm building a windowless menubar application (agent) with a popover. This application requires an integration with Firebase to retrieve some data from this store. XCode version is 6.3.
After installing the latest version of cocoapods (0.38.2) and creating the Podfile as such:
# Uncomment this line to define a global platform for your project
platform :osx, '10.10'
pod 'Firebase', '>= 2.3.3'
target 'dTrain-osx-agent' do
end
target 'dTrain-osx-agentTests' do
end
I'm getting the following build failure error:
Ld /Users/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Products/Debug/dTrain-osx-agent.app/Contents/MacOS/dTrain-osx-agent normal x86_64
cd /Users/user/Projects/interop/dtrain_osx/dTrain-osx-agent
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/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Products/Debug -F/Users/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Products/Debug -F/Users/user/Projects/interop/dtrain_osx/dTrain-osx-agent/Pods/Firebase -filelist /Users/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Intermediates/dTrain-osx-agent.build/Debug/dTrain-osx-agent.build/Objects-normal/x86_64/dTrain-osx-agent.LinkFileList -Xlinker -rpath -Xlinker #executable_path/../Frameworks -mmacosx-version-min=10.10 -ObjC -lc++ -licucore -framework CFNetwork -framework Firebase -framework Security -framework SystemConfiguration -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -Xlinker -add_ast_path -Xlinker /Users/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Intermediates/dTrain-osx-agent.build/Debug/dTrain-osx-agent.build/Objects-normal/x86_64/dTrain_osx_agent.swiftmodule -lPods -Xlinker -dependency_info -Xlinker /Users/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Intermediates/dTrain-osx-agent.build/Debug/dTrain-osx-agent.build/Objects-normal/x86_64/dTrain-osx-agent_dependency_info.dat -o /Users/user/Library/Developer/Xcode/DerivedData/dTrain-osx-agent-djvvfitvallgicbrsrwbnbhaxoqw/Build/Products/Debug/dTrain-osx-agent.app/Contents/MacOS/dTrain-osx-agent
ld: framework not found OpenGLES for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Edit:
XCode upgraded to 6.4 (6E35b) and still facing the problem
It looks like this issue is a result of some changes we made to how we build the Firebase SDK in 2.3.3. For now, can you pleas use the 2.3.2 version of the SDK, by modifying your Podfile as such:
pod 'Firebase', '2.3.2'
Meanwhile we'll investigate and try to get this fixed in a future version of the SDK. I'll update this answer once we do.
Note that if you're using Swift and Firebase SDK 2.3.2, you'll need to use a bridging header to import Firebase into a swift app.
To do that, create a new Objective-C file in your project. When prompted if you would like to include a bridging header, say “yes”. Then, delete the .m file that you created, keeping only the bridging-header.h file that was created for you. Add the following line to that file:
#import <Firebase/Firebase.h>
If you still getting errors after you have done the install and adding the #import .
Try this. Open Terminal, then cd to your project and type Pod Install, it will prompt you, says something like your project was edit by another application, if you want to Revert or Keep The XCode Version. Chose Revert.
This fixed my error. Hope will help others.

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.

XCode with Single View Application (MonoDevelop) Fails Build - Undefined symbols for architecture i386

On the MonoTouch cruise ship wet behind the ears.
Steps I followed the Hellow World tut from the Xamarin site; http://docs.xamarin.com/ios/getting_started/hello_iphone
1) Installed XCode via App Store
2) Installed MonoDevelop fresh from the Xamarin site
Created new project "HelloApple" (Single View Application) in MonoDevelop.
Built the app (command+B).
Opened the generated HelloApple_UIViewController.xib file with XCode Interface Builder
Clicked the Run button.
Received the following error;
Ld
DerivedData/HelloApple.UI/Build/Products/MonoTouch-iphonesimulator/HelloApple.UI.app/HelloApple.UI
normal i386
cd /Users/randerson/Projects/HelloApple/HelloApple.UI/obj/Xcode/1
setenv IPHONEOS_DEPLOYMENT_TARGET 6.0
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Applications/MonoDevelop.app:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.0.sdk
-L/Users/randerson/Projects/HelloApple/HelloApple.UI/obj/Xcode/1/DerivedData/HelloApple.UI/Build/Products/MonoTouch-iphonesimulator
-F/Users/randerson/Projects/HelloApple/HelloApple.UI/obj/Xcode/1/DerivedData/HelloApple.UI/Build/Products/MonoTouch-iphonesimulator
-filelist /Users/randerson/Projects/HelloApple/HelloApple.UI/obj/Xcode/1/DerivedData/HelloApple.UI/Build/Intermediates/HelloApple.UI.build/MonoTouch-iphonesimulator/HelloApple.UI.build/Objects-normal/i386/HelloApple.UI.LinkFileList
-Xlinker -objc_abi_version -Xlinker 2 -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=6.0 -framework UIKit -framework MapKit -framework Foundation -framework CoreGraphics -o /Users/randerson/Projects/HelloApple/HelloApple.UI/obj/Xcode/1/DerivedData/HelloApple.UI/Build/Products/MonoTouch-iphonesimulator/HelloApple.UI.app/HelloApple.UI
Undefined symbols for architecture i386: "_main", referenced from:
start in crt1.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see
invocation)
I have uninstalled XCode and MonoDevelop thinking I did things out of order or something a few times.
Really stumped. I would think the example would work out the box, so I gotta be missing something here.
Several posts on StackOverflow revolve around more complex already built apps, this is adding nothing to the project right out of the box and build.
What am I missing?
You do not build from Xcode itself, you build from MonoDevelop.
The most commonly used part of Xcode is the Interface Builder. You can create your UI using this tool (or using code) and then save it to disk. You then switch back to MonoDevelop to compile verything into a native application.
Note: MonoTouch itself will use parts of Xcode (command line tools) to build the native application. However, for most developers, this is not something you'll be aware of.

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 build error in Xcode project

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?

Resources