linker error Xcode 5 - ios

Can't understand the error.
duplicate symbol _currentCount in:
/Users/selim/Library/Developer/Xcode/DerivedData/iXEN-aimjepotqgbjmlaghqjovwpsngvx/Build/Intermediates/iXEN.build/Debug-iphonesimulator/iXEN.build/Objects-normal/i386/Server.o
/Users/selim/Library/Developer/Xcode/DerivedData/iXEN-aimjepotqgbjmlaghqjovwpsngvx/Build/Intermediates/iXEN.build/Debug-iphonesimulator/iXEN.build/Objects-normal/i386/Alerts.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1、if you declare the currentCount in .h file and include it in two .m file.
Add extern in front of currentCount declaration in .h file.
example
extern int currentCount;
2、if you declare the currentCount in two .m file.
And static in front of currentCount in .m file
example
static int currentCount
One more thing, variable declare after #implementation doesn't belong to that class , it is global value.
#interface Obj : NSObject
#end
#implementation Obj
int a = 0 ; // a declare in Obj class
#end
#interface Obj2 : NSObject
#end
#implementation Obj2
- (id)init
{
self = [super init] ;
if (self) {
a = 1 ; // you can access it in Obj2 class
}
return self ;
}
#end

Add Quartzcore framework
or check the file you don't have any duplicate file name in project.i think you add two projects thats why this error is occur.

Check it may you write a "#import file.m" instead of "#import file.h". So, In Compiles Resource will duplicate symbol file.o.
You may need to remove the duplicates in Targets Build Phases under the Compiled Sources grouping.

Related

Using inline C function is impossible in initializer marked via NS_DESIGNATED_INITIALIZER

Say I have a class like this
// DummyObject.h
#interface DummyObject : NSObject
- (instancetype)initWithFoo:(NSString *)foo
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
#end
and
// DummyObject.m
inline void
foobar() {}
static inline void
static_foobar() {}
#implementation DummyObject
- (instancetype)initWithFoo:(NSString *)foo
{
if (self = [super init])
{
// Undefined symbols for architecture x86_64:
// "_foobar", referenced from:
// -[DummyObject initWithFoo:] in DummyObject.o
foobar();
static_foobar(); // Works as expected
}
return self;
}
#end
My understanding about inline is that it simply suggest the compiler to embed the function body into the calling code instead of executing an actual call. And static refers to the scope, which means I can only use this very function inside the file that defines it.
Update 1:
According to trungduc's comment, "inline foobar" works after change DummyObject.m to DummyObject.mm.
So my current conclusion is
If DummyObject is compiled as Objective-C(.m), then "inline foobar" won't work
If DummyObject is compiled as Objective-C++(.mm), then "inline foobar" works fine
If I delete the "inline" keyword, then "foobar" works fine in both Objective-C and Objective-C++
"static inline static_foobar" always works
This is quite confusing, I've checked the Clang documentation with no luck. This has been in my head for about 2 days, any suggestions will help...

Objective C - Custom struct 'make' method similar to CLLocationCoordinate2DMake

I've written a custom struct in a separate header file. It looks something like this
typedef struct RequestSpecifics {
BOOL includeMetaData;
BOOL includeVerboseData;
} RequestSpecifics;
Now I want to make a custom 'make' method, similar to the CoreLocation struct CLLocationCoordinate2 CLLocationCoordinate2DMake method.
I've tried two different ways. While both ways give no errors in the .h file, I do get errors when I want to use the make method.
Method 1:
extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData);
Throws:
Apple Mach-O Linker
"_RequestSpecificsMake", referenced from:
Error Linker command failed with exit code 1 (use -v to see invocation)
Method 2:
extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
RequestSpecifics specifics;
specifics.includeMetaData = includeMetaData;
specifics.includeVerboseData = includeVerboseData;
return specifics;
}
Throws:
Apple Mach-O Linker
Error Linker command failed with exit code 1 (use -v to see invocation)
Usage example:
RequestSpecificsMake(NO, NO)
I've checked all common solutions for the Apple Macho-Linker error but nothing seems to work or the solutions are not relevant.
So how do I correctly implement the 'make' method for a struct?
So apparently method 2 should be the implementation and it should not be in the .h file. Naturally, I need a .m file as well. This should be the correct way to do it:
.h file
RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData);
.m file
RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
RequestSpecifics specifics;
specifics.includeMetaData = includeMetaData;
specifics.includeVerboseData = includeVerboseData;
return specifics;
}
In the end I had to combine both methods! Also, by the looks of it, the extern keyword is not required.
Why dont you try
static inline instead of extern
static inline RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
RequestSpecifics specifics;
specifics.includeMetaData = includeMetaData;
specifics.includeVerboseData = includeVerboseData;
return specifics;
}
or if you want to use extern then you need to write it in .m file.

Swift - Bridging header failed. '*' does not have a member named '*'

I develop an app that using CommonCrypto library. The problem is I can create an instance in Swift file. My object created using Objective- C. It seems can't create bridging header very well.
Error message
/Users/MNurdin/Documents/iOS/xxxxx/Models/Main.swift:15:9: 'CustomObject' does not have a member named 'encrypt'
CustomObject.h
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
#import "GTMBase64.h"
#interface CustomObject : NSObject
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key;
#end
CustomObject.m
#import "CustomObject.h"
#implementation CustomObject
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key{
/*--*/
return result;
}
#end
Global.swift
var instanceOfCustomObject: CustomObject = CustomObject()
println(instanceOfCustomObject.encrypt("p#$$w0rd","12345678"))
The initial + in the declaration indicates that
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key;
is a class method in Objective-C. You have to call it on the
class (or type in Swift linguage) itself, not on an instance:
let encrypted = CustomObject.encrypt("p#$$w0rd", withKey: "12345678")

Problems with Protocols in Categories (clang)

I have some problems with Protocols in Categories with clang compilier in Objective-C.
I think clang ignore any protocols if this specified in Category, like in sample code below.
If line "#interface MyClass() <MyProtocol>" is replaced to "#interface MyClass()" than object files is absolutely same (byte-to-byte).
#include <stdio.h>
#include <Foundation/Foundation.h>
#protocol Protocol1
#end
#protocol MyProtocol
#end
#interface MyClass : NSObject<Protocol1>
#end
#interface MyClass() <MyProtocol>
#end
#implementation MyClass
#end
int main() {
if ([MyClass conformsToProtocol:#protocol(Protocol1)])
printf("Protocol1 is conformed\n");
if ([MyClass conformsToProtocol:#protocol(MyProtocol)])
printf("MyProtocol is conformed\n");
return 0;
}
I compilied this code on my Ubuntu and I got next output:
$ clang-3.5 -o main main.m -I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` -lgnustep-base -fconstant-string-class=NSConstantString -D_NATIVE_OBJC_EXCEPTIONS -lobjc
$ ./main
Protocol1 is conformed
But if this code is buid on OS X, I will get next ouput:
$ clang -o main main.m -lobjc
$ ./main
Protocol1 is conformed
MyProtocol is conformed
This problem is related with options -fobjc-runtime=gnustep and -fobjc-runtime=macosx, but I don't know why.
How can I fix this problem with gnustep environment? What can I do?

Unity Scripting armv7 Error

I'm attempting to write my first unity script. This is the code for a file called TestPlugin.cs that is located in Assets/Plugins:
using UnityEngine;
using System.Runtime.InteropServices;
public class TestPlugin : MonoBehaviour
{
[DllImport ("__Internal")]
private static extern int getString ();
public static void Awake () {
print (getString ());
}
}
This is the code for two files that I import into the generated xCode project's classes folder:
TestPlugin.h:
#import <Foundation/Foundation.h>
#interface TestPlugin : NSObject
-(int)getString;
#end
TestPlugin.m:
#import "TestPlugin.h"
#implementation TestPlugin
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (int)getString
{
return 7;
}
#end
Finally this is the javascript file that sits inside the Asset folder.
TestPluginTest.js:
function Update ()
{
TestPlugin.Awake ();
}
Also, please note that i'm not necessarily expecting this all to work, just to compile at this point (though extra pointers and tips are welcome)
The error I get in xCode when trying to build onto iPhone (actual device) is this:
Undefined symbols for architecture armv7: "_getString", referenced
from:
RegisterMonoModules() in RegisterMonoModules.o ld: symbol(s) not found for architecture armv7 collect2: ld returned 1 exit status
"_getString", referenced from:
RegisterMonoModules() in RegisterMonoModules.o
ld: symbol(s) not found for architecture armv7
collect2: ld returned 1 exit status
I'm stumped! Thanks in advance!
I think the problem lies in the Obj-C interface because the linker does not know how to handle the signature. When I connected a self written library I designed the interface to contain pure C code only:
interface.h
#ifdef __cplusplus
extern "C" {
#endif
int getString();
#ifdef __cplusplus
}
#endif
interface.c:
int getString() {
// do something
}
Maybe useful blog postings:
iPhone & Unity3D: Integrating 3rd Party Static Libraries in Unity3D Generated XCode Projects
Unity Native Plugins: OS X
Clever Martian's Blog - An Experiment with iPhone Native UI and Unity 3 Pro

Resources