Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate Symbol Error from C functions in Objective-C

First off, I have only really used Objective-c methods in my programming. I decided to do a couple of quick math calculations as c functions and then ended up needing them for multiple classes. So then I stuck the c functions in a separate .h file. This works fine until I try and import the .h file into more than one class. Then I get this error:

Duplicate Symbol *_myFunction* blah blah blah Linker command failed with exit code 1 (use -v to see invocation)

How can I use a c function in more than one class without this link error. I've tried just defining the functions in the classes I need them in, but it seems that even if they are different classes, I get this error if the function name is the same. I'm probably crazy here, but some help understanding would be great.

like image 905
daveMac Avatar asked May 05 '12 00:05

daveMac


1 Answers

You should put declarations in the .h file, make them extern, and move definitions into a .c or .m file.

From this

myfunctions.h

int max(int a, int b) {
    return a>b ? a : b;
}

Move to this:

myfunctions.h

extern int max(int a, int b); // declaration

myfunctions.c

int max(int a, int b) {
    return a>b ? a : b;
}
like image 83
Sergey Kalinichenko Avatar answered Oct 14 '22 10:10

Sergey Kalinichenko