xcode Xcode重复符号错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11773974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode duplicate symbol error
提问by user1570730
I am getting "Apple Mach-O Linker (Id) Error":
我收到“Apple Mach-O 链接器(ID)错误”:
ld: duplicate symbol _matrixIdentity in /BlahBlah/Corridor.o and /Blahblah/Drawable.o for architecture i386
The class "Corridor" is extending the class "Drawable" and "_matrixIdentity" is defined and implemented in a file "Utils.h". Here are top lines from my header files:
“Corridor”类扩展了“Drawable”类,“_matrixIdentity”在文件“Utils.h”中定义和实现。以下是我的头文件中的顶行:
Drawable.h
可绘制文件
#import <Foundation/Foundation.h>
#import "Utils.h"
@interface Drawable : NSObject
...
Corridor.h
走廊.h
#import <Foundation/Foundation.h>
#import "Drawable.h"
@interface Corridor : Drawable
...
I have already checked if there are any ".m" imports instead of ".h", everything is correct. Any idea, what could cause this problem?
我已经检查过是否有任何“.m”导入而不是“.h”,一切都是正确的。任何想法,什么可能导致这个问题?
EDIT: posting code from "Utils.h"
编辑:从“Utils.h”发布代码
#import <Foundation/Foundation.h>
...
#pragma mark -
#pragma mark Definitions
typedef float mat4[16];
#pragma mark -
#pragma mark Functions
void matrixIdentity(mat4 m)
{
m[0] = m[5] = m[10] = m[15] = 1.0;
m[1] = m[2] = m[3] = m[4] = 0.0;
m[6] = m[7] = m[8] = m[9] = 0.0;
m[11] = m[12] = m[13] = m[14] = 0.0;
}
...
I am only referencing to "mat4" definition in my both classes' methods. Also, "matrixIdentity" is just the first function in this file, may be the problem is not in implementation.
我只在我的两个类的方法中引用了“mat4”定义。此外,“matrixIdentity”只是这个文件中的第一个函数,可能是问题不在实现中。
采纳答案by booiljoung
C/C++/Objective-C diff with Java, C#, Ruby, Python...
C/C++/Objective-C 与 Java、C#、Ruby、Python 的差异...
Divide files.
分割文件。
header & mm
标题和毫米
Do not use #include (may include many times)
不要使用#include(可能包含多次)
Use #import... (include once)
使用 #import...(包括一次)
Utils.h
实用程序
#ifndef __utils_h__ // <<< avoid multiple #include
#define __utils_h__ // <<< avoid multiple #include
#import <Foundation/Foundation.h>
...
#pragma mark -
#pragma mark Definitions
typedef float mat4[16];
#pragma mark -
#pragma mark Functions
extern void matrixIdentity(mat4 m);
#endif // __utils_h__ <<< avoid multiple #include
Utils.mm
实用程序.mm
#import "Utils.h"
void matrixIdentity(mat4 m)
{
m[0] = m[5] = m[10] = m[15] = 1.0;
m[1] = m[2] = m[3] = m[4] = 0.0;
m[6] = m[7] = m[8] = m[9] = 0.0;
m[11] = m[12] = m[13] = m[14] = 0.0;
}
...
回答by booiljoung
Two solutions to your problem:
您的问题的两种解决方案:
- Declare only
void matrixIdentity(mat4 m);
in the header file and then implment the actual code in a corresponding c/m file. Make your function in the header file inline (that's the technique Apple uses)
inline void matrixIdentity(mat4 m) { ...
- 仅
void matrixIdentity(mat4 m);
在头文件中声明,然后在相应的 c/m 文件中实现实际代码。 内联头文件中的函数(这是 Apple 使用的技术)
inline void matrixIdentity(mat4 m) { ...
回答by marko
From your description, utils.h declares and implements a variable, the implementation of which is compiled in corridor.h and Drawable.h by virtue of utils.h being included in both (indirectly through Drawable.h in the case of Corridor.h). Thus both compilation units contain an implementation for _matrixIdentity, and the linker complains.
根据您的描述,utils.h 声明并实现了一个变量,由于 utils.h 包含在两者中(在 Corridor.h 的情况下间接通过 Drawable.h )。因此,两个编译单元都包含 _matrixIdentity 的实现,并且链接器会抱怨。
Move the implementation of _matrixIdentity into a new module utils.m to ensure there is only one definition of the symbol.
将 _matrixIdentity 的实现移动到一个新模块 utils.m 中,以确保只有一个符号定义。
回答by Deepak Kumar
Use -force_load for one library in Other linker flags .. that solved the prob for me once
对其他链接器标志中的一个库使用 -force_load .. 为我解决了一次问题
回答by rounak
In my case, I was implementing a function in the header file itself. Adding a static inline keyword before the function fixed the error for me.
就我而言,我在头文件本身中实现了一个函数。在函数之前添加一个静态内联关键字为我修复了错误。