Objective-C 链接错误和重复符号错误

发布于 2024-11-08 15:20:11 字数 1091 浏览 0 评论 0原文

我知道这里有类似的问题,但我无法解决。

我有一个视图,它在实现上面的 .m 中,

CFDataRef CopyImagePixels(CGImageRef inImage){
    return CGDataProviderCopyData(CGImageGetDataProvider(inImage));
}

一切正常,但我有另一个视图,需要访问相同的函数,如果我将它放在 /that/views.m 文件中,相同的位置..编译器抛出重复的符号错误。

这两个文件唯一的共同点是:

#import "MopalAppDelegate.h"

我想也许将代码放在 AppDelegate 中,但是当我尝试构建时,它会抛出相同的错误。

我对 obj 还很陌生-c,任何建议都会非常好赞赏。

谢谢。

编辑:错误:

ld: duplicate symbol _CopyImagePixels in  /Users/critter/Library/Developer/Xcode/DerivedData/Mopal-dtgtjbahdowmuderbstlmsiznwsi/Build/Intermediates/Mopal.build/Debug-iphonesimulator/Mopal.build/Objects-normal/i386/ViewerController.o and /Users/critter/Library/Developer/Xcode/DerivedData/Mopal-dtgtjbahdowmuderbstlmsiznwsi/Build/Intermediates/Mopal.build/Debug-iphonesimulator/Mopal.build/Objects-normal/i386/StudyListDetailController.o for architecture i386
collect2: ld returned 1 exit status
Command /Xcode4/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

I know there are questions on here similar to this, but I have not been able to sort it out.

I have a view that has this in the .m above the implementation

CFDataRef CopyImagePixels(CGImageRef inImage){
    return CGDataProviderCopyData(CGImageGetDataProvider(inImage));
}

Everything works fine, but I have another view that needs to access that same function, if I put it in /that/ views .m file, same location.. the compiler throws a duplicate symbol error..

The only file these two have in common that they include is:

#import "MopalAppDelegate.h"

I thought maybe putting the code in the AppDelegate, but that throws the same error when I attempt to build..

I am still quite new to obj-c, any suggestions would be greatly appreciated.

Thanks.

Edit: The error:

ld: duplicate symbol _CopyImagePixels in  /Users/critter/Library/Developer/Xcode/DerivedData/Mopal-dtgtjbahdowmuderbstlmsiznwsi/Build/Intermediates/Mopal.build/Debug-iphonesimulator/Mopal.build/Objects-normal/i386/ViewerController.o and /Users/critter/Library/Developer/Xcode/DerivedData/Mopal-dtgtjbahdowmuderbstlmsiznwsi/Build/Intermediates/Mopal.build/Debug-iphonesimulator/Mopal.build/Objects-normal/i386/StudyListDetailController.o for architecture i386
collect2: ld returned 1 exit status
Command /Xcode4/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔小萌 2024-11-15 15:20:11

您的函数正在全局范围内声明和定义。当您将相同的函数定义放在两个不同的 .m 文件中时,最终会得到两个不同的函数(就编译器而言),它们在同一作用域(全局)中具有相同的名称,当您说 CopyImagePixels(myImage); 时,它无法知道您打算调用哪一个。因此出现错误消息。

所以有几个选择。一种是将您的函数定义为使用它的对象上的私有成员函数,例如:

@implementation MyClass

- (CFDataRef) copyImagePixels: (CGImageRef)inImage {
    return CGDataProviderCopyData(CGImageGetDataProvider(inImage));
}

@end

另一种(不太优选)选项是将您的函数声明移动到共享头文件,例如MopalAppDelegate.h 及其在 MopalAppDelegate.m 文件中的实现。这将创建一个可以共享给其他类的函数声明,并在 .m 文件中使用单个定义。

Your function is being declared and defined in the global scope. When you place the same function definition in two different .m files, you end up with two different functions (as far as the compiler is concerned) that have the same name in the same scope (global), leaving it with no way of knowing which one you intend to call when you say CopyImagePixels(myImage);. Hence the error message.

So there are a couple of options. One is to define your function as a private member function on the objects that use it, like:

@implementation MyClass

- (CFDataRef) copyImagePixels: (CGImageRef)inImage {
    return CGDataProviderCopyData(CGImageGetDataProvider(inImage));
}

@end

The other (and less preferred) option is to move your function declaration to a shared header file, like MopalAppDelegate.h, and its implementation into your MopalAppDelegate.m file. This creates a function declaration that can be shared out to other classes, with a single definition in the .m file.

方觉久 2024-11-15 15:20:11

您希望将函数定义放在两个文件共享的标头中,在相应的 .m 文件中实现它,然后将该标头导入到两个视图中。

但如果这就是该函数的全部功能,为什么不在这两个地方直接调用 CGDataProviderCopyData 呢?

You want to put the function definition in a header that the two files share, implement it in the corresponding .m file, and import that header into both views.

But if that's all the function does, why not just call CGDataProviderCopyData directly in both places?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文