OCMock link error - xcode

I'm trying to get a simple OCMock test for OSX up and running, but can't seem to get the install right. I believe I've followed the instructions, but the test build is failing at the link step.
Within fooTests.m:
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#interface fooTests : XCTestCase
#end
#implementation fooTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testOCMockPass {
id mock = [OCMockObject mockForClass:NSString.class];
[[[mock stub] andReturn:#"mocktest"] lowercaseString];
NSString *returnValue = [mock lowercaseString];
STAssertEqualObjects(#"mocktest", returnValue, #"Should have returned the expected string.");
}
#end
But on build, I get the following warnings/errors:
fooTests.m:56:5: Implicit declaration of function 'STAssertEqualObjects' is invalid in C99
Undefined symbols for architecture x86_64:
"_STAssertEqualObjects", referenced from:
-[fooTests testOCMockPass] in fooTests.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've got "Link Binary with Library" linking with XCTest & OCMock in the test build phase. OCMock.framework is in the base directory, which is also in the search path for frameworks and for headers. Can anyone assist in telling me what I'm doing wrong please?
It looks as though the compiler doesn't know what STAssertEqualObjects() is (i.e. I haven't included something) and hence it doesn't know what to link with, hence the other 2 errors. I just don't know what else I need to include.

This is not an OCMock problem. You are using XCUnit (the Xcode 5 unit testing framework), but you are calling STAssertEqualObjects (from OCUnit, the Xcode 4 unit testing framework). Simply change that to XCTAssertEqualObjects.

Related

Creating NSManagedObject subclass leads to linker error duplicate symbols

I am trying to create a nsmanagedobject (User) by going to the Editor menu and then selecting Create NSManagedObject Subclass...this generates four files:
User+CoreDataClass.h
User+CoreDataClass.m
User+CoreDataProperties.h
User+CoreDataProperties.m
In one of my viewcontrollers I import User+CoreDataClass.h and then have this code:
//create new account entity
User* thisUser = [NSEntityDescription
insertNewObjectForEntityForName:#"User"
inManagedObjectContext:self.myController.myDataManager.managedObjectContext];
When I then build the app, I get the following clang error:
duplicate symbol _OBJC_CLASS_$_User in:
/Users/xxxxxxxxxxxx/Library/Developer/Xcode/DerivedData/StarDate-fzkjccyoiwhfvvczdwkvkmtbioqw/Build/Intermediates/StarDate.build/Debug-iphonesimulator/StarDate.build/Objects-normal/x86_64/User+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_User in:
/Users/xxxxxxxxxxxx/Library/Developer/Xcode/DerivedData/StarDate-fzkjccyoiwhfvvczdwkvkmtbioqw/Build/Intermediates/StarDate.build/Debug-iphonesimulator/StarDate.build/Objects-normal/x86_64/User+CoreDataClass.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried cleaning the app and deleting my derived data but that had no results. The only way I can clear the error is to delete the generated files. Which obviously is not going to be what I need. Why is XCode creating duplicate files off this menu command? Is there a setting that I missed?
Thanks
You are manually creating NSManagedObject subclasses, that Xcode 8 already has created for you and thus get duplicated symbols.
You can find detailed information how to solve this in this answer.

Swift: Undefined Symbols: iTunesApplication

I'm trying to create Swift OS X app now, and found difficulty using ScriptingBridge.
I included proper iTunes.h file, and Xcode is not giving any error when I wrote "iTunesApplication" as type.
However, when I compile(run) the app, it gives me error :(
Does anybody know about this issue?
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_iTunesApplication", referenced from:
__TFC12LoveYouChloe11AppDelegate10showWindowfS0_FPSs9AnyObject_T_ 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)
And here is my code:
var iTunes: iTunesApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") as iTunesApplication
iTunes.playpause()
The best way to solve this is to take the generated Objective-C Scripting Bridge header and convert it into a native Swift. I wrote a Python script (here) that can do that for you. You can see my answer here for a better explanation of what exactly is going on if you're interested.
Instead of
var iTunes: iTunesApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")
use
var iTunes: AnyObject = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")
Sometimes the compiler gets confused when you are accessing two or more properties in a row(no idea why) so you may need to use a temporary variable. E.g.:
Instead of:
let songName: String = iTunes.playlists[0].songs[0].name
Try:
let song: AnyObject = iTunes.playlists[0].songs[0]
let songName = song.name
Or Alternatively the faster (this also works if you declare iTunes as an SBApplication) :
let songName: String = iTunes.valueAtIndex(0, inPropertyWithKey: "playlists").valueAtIndex(0, inPropertyWithKey: "songs").valueForKey("name")
EDIT:
You can also generate the .swift headers as specified here: https://github.com/tingraldi/SwiftScripting

Objective C interface declaration

i'm trying a simple academic program where an interface is declared as:
#import <objc/Object.h>
#interface Saludador:Object{
char* saludo;
}
- init;
- (void)setSaludo:(char*)unSaludo;
- (void)setSaludo:(char*)unSaludo y:(char*)unaColetilla;
- (void)saluda;
#end
When I try to compile the .m file i get the error:
error: cannot find interface declaration for 'Object', superclass of 'Saludador'
I really don't know why, i'm compiling on the terminal window in a mac OSX 10.9.
thanks for the help
Object is the OBJC_ROOT_CLASS for ObjC 1.0, for ObjC 2.0 use NSObject and #import <Foundation/Foundation.h>. You'll also have to add the -framework Foundation as a compiler flag if you are compiling using clang or gcc on the command line.
See the header file Object.h:
#if __OBJC__ && !__OBJC2__
__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_NA)
OBJC_ROOT_CLASS
#interface Object
Note the #if __OBJC__ && !__OBJC2__.
EDIT:
I actually managed to find that: When writing code that is based upon the Foundation framework, that root class is typically NSObject in an old document... The OBJC_ROOT_CLASS got me confused, so even if you're using ObjC 1.0 it's still NSObject.

Xcode error "Duplicate Symbol" causing Apple Mach-O Linker Error

duplicate symbol _leagueTableLoaded in:
/Users/Brendan/Library/Developer/Xcode/DerivedData/2013-dbhrwzgxgwhfbqatgqpfrmqyucyu/Build/Intermediates/2013.build/Debug-iphonesimulator/2013.build/Objects-normal/i386/LTGlobalResultsViewController.o
/Users/Brendan/Library/Developer/Xcode/DerivedData/2013-dbhrwzgxgwhfbqatgqpfrmqyucyu/Build/Intermediates/2013.build/Debug-iphonesimulator/2013.build/Objects-normal/i386/LTJumpToMeViewController.o
duplicate symbol _showGLobalCompany in:
/Users/Brendan/Library/Developer/Xcode/DerivedData/2013-dbhrwzgxgwhfbqatgqpfrmqyucyu/Build/Intermediates/2013.build/Debug-iphonesimulator/2013.build/Objects-normal/i386/LTGlobalResultsViewController.o
/Users/Brendan/Library/Developer/Xcode/DerivedData/2013-dbhrwzgxgwhfbqatgqpfrmqyucyu/Build/Intermediates/2013.build/Debug-iphonesimulator/2013.build/Objects-normal/i386/LTJumpToPositionViewController.o
duplicate symbol _leagueTableLoaded in:
/Users/Brendan/Library/Developer/Xcode/DerivedData/2013-dbhrwzgxgwhfbqatgqpfrmqyucyu/Build/Intermediates/2013.build/Debug-iphonesimulator/2013.build/Objects-normal/i386/LTGlobalResultsViewController.o
/Users/Brendan/Library/Developer/Xcode/DerivedData/2013-dbhrwzgxgwhfbqatgqpfrmqyucyu/Build/Intermediates/2013.build/Debug-iphonesimulator/2013.build/Objects-normal/i386/LTJumpToPositionViewController.o
ld: 3 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am getting the error above in xcode only when I try to build in simulator (on any iOS device I can build without error). I have three classes:
1.LTGlobalResultsViewController
2.LTJumpToMeViewController
3.LTJumpToPositionViewController
All three were created in xcode but both 2 & 3 have been modified outside of xcode and then rebuilt.
The #property bool leagueTableLoaded is defined in the header for all three. it is declared as:
#property bool leagueTableLoaded;
What is it exactly that is causing this error? I have tried the following:
I have tried renaming leagueTableLoaded in different classes but this doesn't fix it.
I have tried deleting my Derived Data files manually in library/developer/xcode folder.
According to other questions I have checked if I am importing a .m file. This is not the case. Apple Mach-O Linker error ("duplicate symbol")
Any other suggestions or advice?
Thanks,
James
ADDITION: As requested please find all the extracts from my .h and .m files that reference leagueTableLoaded or any of the variants I created when trying to get round this error:
LTJumpToMeViewController.h
#property bool leagueTableLoadedMe;
LTJumpToMeViewController.m
#implementation LTJumpToMeViewController
bool leagueTableLoaded = false;
LTGlobalResultsViewController.h
#property bool globalLeagueTableLoaded;
LTGlobalResultsViewController.m
#implementation LTGlobalResultsViewController
bool leagueTableLoaded = false;
LTJumpToPositionViewController.h
#property bool leagueTableLoadedPos;
LTJumpToPositionViewController.m
#implementation LTJumpToPositionViewController
bool leagueTableLoaded = false;
I can provide more information if required!
For me a duplicate symbol error came up when I absent mindedly included a .m file instead of a .h (Why does Xcode's autosuggest even show me .m files?!)
In this end this was being caused by the leagueTableLoaded bool being defined in both LTGlobalResultsViewController.m and LTJumpToPositionViewController.m.
Removing it from one of them fixed the issue. Although I'm not sure why it was there in the first place! Hope this helps anyone else who experiences the same issue! James
Xcode error “Duplicate Symbol” causing Apple Mach-O Linker Error is caused by duplicate symbols in Project. Steps to avoid error
Go to project -> Target ->Build Phases ->Compile sources
Check for the duplicate file (implementation file)
Delete the file and add it again
Clean and run project again
This worked for me. Hope it helps
In second view controller, you mistakenly #import "First.m", check it must be first.h file so replaces this with first.h. It's working for me.

Xcode linker and blocks: Undefined symbol "___block_global_1"

I am trying to build an application in Xcode 3.2.4 and am getting the following linker error:
Undefined symbols:
"___block_global_1", referenced from:
___block_holder_tmp_1.120 in foobarbaz.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I'm at a loss to explain what I've done in my source file that might be causing the error. I do have a block that I am defining as a global variable, like so:
typedef void(^error_block_t)(NSError* error);
error_block_t error_handler_s = ^void(NSError* error)
{
//...
}
This block is defined in an empty namespace in the source (I'm compiling Objective-C++.) Everything compiles without error.
Update: Moving the block to be a local variable for the routine that references it is a viable (though not preferred) workaround.
What gives?
If the error_handler_s is not intended to be exported, you could make it static as another workaround.
namespace {
...
static error_block_t error_handler_s = ^void(NSError* error) { ... };
...
}
Otherwise, I believe this is a bug in gcc.
At this point I believe this issue to be a bug.

Resources