Undefined symbols for architecture

I ran across my favorite error again today. Often times these are really difficult to understand if you don’t know what they are referring to. Here’s the one that I got:

Undefined symbols for architecture x86_64:

  “_CATransform3DMakeRotation”, referenced from:

      -[SOFViewController updateScaleName] in SOFViewController.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Turns out this is super easy. In this case, the symbol is undefined because the QuartzCore library isn’t included, despite the inclusion of #include <QuartzCore/QuartzCore.h>.

The binary needs to be linked with the library, conveniently in the “link binary with libraries” section under build phases in Xcode.

Shortcuts

Hey, shortcuts are nice, but what about this Objective-C monstrosity:

NSMutableArray *returnArray = [@[] mutableCopy];

Guh. I kind of wrote this because I could, but I think this is much better:

NSMutableArray *returnArray = [NSMutableArray new];

While we’re at it, since this is for a function that is going to return an immutable NSArray, why not go for a name that reflects what is actually going on?

NSMutableArray *mutableReturnArray = [NSMutableArray new];

Finally, when it’s time:

return [mutableReturnArray copy];

A word of warning, this last step seems to be unnecessary for the running of an app, but I’m told that this doesn’t make much sense, since the debugger will still see this as a mutable array. It might seem pedantic, but it’s apparently correct. Has this ever bitten you?

Personal Milestone

I’ve been using git for about a year now. Today, I refactored some code and hit a personal milestone:

9 files changed, 153 insertions(+), 162 deletions(-)

First time that a refactor has removed more code than it’s inserted. I feel pretty good about that, but it was pretty easy, seeing as there was a bunch of redundant, encapsulated stuff that had no business spanning multiple methods.

Still, here’s to brevity.

Linker Command Failed

Linker Command turns one year old today, and I’m in my last days at the place that I started it for the Summer, so I wanted to share the error behind the name.

Last summer, I was getting really angry at seeing this error come up over and over again. If you’re new to C like I am, and you something that starts like this:

Ld /Volumes/Storage/Users/mike/Library/Developer/Xcode/DerivedData/PedalCharts-gvryltpjsvqnwraultwngecnzbsd/Build/Products/Debug-iphonesimulator/PedalCharts.app/PedalCharts normal x86_64

    cd /Volumes/Storage/Users/mike/Documents/Cocoa/Bitbucket/PedalCharts

    export IPHONEOS_DEPLOYMENT_TARGET=7.0

    export PATH=“/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin”

    ETC ETC ETC

You are going to look at this and say, I have no idea what to do with this garbage. The important stuff is in the second half (and Xcode is much more forthcoming with this than it used to be):

duplicate symbol _OBJC_METACLASS_$_PedalChart in:

    /Volumes/Storage/Users/mike/Library/Developer/Xcode/DerivedData/PedalCharts-gvryltpjsvqnwraultwngecnzbsd/Build/Intermediates/PedalCharts.build/Debug-iphonesimulator/PedalCharts.build/Objects-normal/x86_64/PCStaffView.o

    /Volumes/Storage/Users/mike/Library/Developer/Xcode/DerivedData/PedalCharts-gvryltpjsvqnwraultwngecnzbsd/Build/Intermediates/PedalCharts.build/Debug-iphonesimulator/PedalCharts.build/Objects-normal/x86_64/PedalChart-45484887584B8F82.o

duplicate symbol _OBJC_CLASS_$_PedalChart in:

    /Volumes/Storage/Users/mike/Library/Developer/Xcode/DerivedData/PedalCharts-gvryltpjsvqnwraultwngecnzbsd/Build/Intermediates/PedalCharts.build/Debug-iphonesimulator/PedalCharts.build/Objects-normal/x86_64/PCStaffView.o

    /Volumes/Storage/Users/mike/Library/Developer/Xcode/DerivedData/PedalCharts-gvryltpjsvqnwraultwngecnzbsd/Build/Intermediates/PedalCharts.build/Debug-iphonesimulator/PedalCharts.build/Objects-normal/x86_64/PedalChart-45484887584B8F82.o

ld: 2 duplicate symbols for architecture x86_64

Along with, finally, the familiar:

clang: error: linker command failed with exit code 1 (use -v to see invocation)

The important part here is the duplicate symbols. The linker is part of the C architecture that links the functions and variables you write with the places they are used in your code. In this case, the linker is complaining that the class PedalChart has been defined in two places. Today, it’s because I used this typo in my PCStaffView Class:

#import “PedalChart.m”

So, this is a big problem, because, instead of importing the header with the declaration of the class, I’ve tried to import the implementation file. The linker doesn’t like this and will throw the error on build time. This is the most likely cause of the error for me, and it’s due to a typo that can be autocompleted.

Personally, I don’t know why you’d want to link an implementation file, but much of this is way over my head anyway.