Objective-C中C函数的重复符号错误

时间:2021-03-05 19:58:16

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:

首先,我在编程中只使用了Objective-c方法。我决定做几个快速的数学计算作为c函数,然后最终需要它们用于多个类。所以我把c函数放在一个单独的.h文件中。这工作正常,直到我尝试将.h文件导入多个类。然后我收到这个错误:

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

重复符号* _myFunction * blah blah blah链接器命令失败,退出代码为1(使用-v查看调用)

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.

如何在没有此链接错误的情况下在多个类中使用c函数。我试过在我需要的类中定义函数,但似乎即使它们是不同的类,如果函数名是相同的,我也会得到这个错误。我在这里可能很疯狂,但有些帮助理解会很棒。

1 个解决方案

#1


14  

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

您应该在.h文件中放置声明,将它们设置为extern,并将定义移动到.c或.m文件中。

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;
}

#1


14  

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

您应该在.h文件中放置声明,将它们设置为extern,并将定义移动到.c或.m文件中。

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;
}