【问题标题】:Apple Mach-O linker error with duplicate symbols even after linking the .m class files to the build phases in Xcode即使在将 .m 类文件链接到 Xcode 中的构建阶段后,Apple Mach-O 链接器也会出现重复符号错误
【发布时间】:2013-04-26 20:00:55
【问题描述】:

当我构建并运行我的项目时,我得到的唯一错误是 Apple Mach-O 链接器错误

架构 i386 的 20 个重复符号

链接器命令失败,退出代码为 1(使用 -v 查看调用)

问题是我已经检查了我的项目中是否存在任何重复的类文件、图片、音频文件(没有)。我已将所有 .m 文件导入到构建阶段-> 编译源。在与二进制库链接中导入正确的框架,并检查图片、xib、音频文件是否已在复制包资源中正确导入 到目前为止,一切看起来都不错。但事实并非如此,当我尝试运行该项目时,链接器错误仍然存​​在。谁能告诉我哪里出错了?

 Ld /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator/Taukytable.app/Taukytable normal i386
        cd "/Users/User/Desktop/Taukytable Newone"
        setenv IPHONEOS_DEPLOYMENT_TARGET 6.0
        setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk -L/Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator -F/Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator -filelist /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Taukytable.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -fobjc-arc -fobjc-link-runtime -Xlinker -no_implicit_dylibs -stdlib=libc++ -mios-simulator-version-min=6.0 -framework CoreAudio -framework CoreMedia -framework CoreFoundation -framework Accelerate -framework CoreData -framework CoreVideo -framework Security -framework CoreImage -framework MessageUI -framework MobileCoreServices -framework AVFoundation -framework QuartzCore -framework SystemConfiguration -framework OpenGLES -framework AssetsLibrary -lsqlite3.0 -framework AudioToolbox -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Products/Debug-iphonesimulator/Taukytable.app/Taukytable
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/MasterViewController.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/PAsavee.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/Preview.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/NSObject+ShareClass.o
    duplicate symbol _vvvalue in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    duplicate symbol _SavedImage in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    duplicate symbol _SavedImage1 in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    duplicate symbol _lll in:
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/AppDelegate.o
        /Users/User/Library/Developer/Xcode/DerivedData/Taukytable-bwphmaegucpyjhfinxfulunsuhag/Build/Intermediates/Taukytable.build/Debug-iphonesimulator/Taukytable.build/Objects-normal/i386/CustomOverlayView.o
    ld: 20 duplicate symbols for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

【问题讨论】:

  • 您是否到处#include 或#import 一个“AppDelegate.m”文件? (您应该只使用 .h 导入标头,而不是 .m)。这也将创建一组重复的符号。还可以尝试清理您的构建产品文件夹。
  • @WarrenP:是的......也检查了这一点并清理了项目。它仍然存在......你可以通过我的问题附上的项目。
  • 尝试退出你的 xcode 并重新启动它。
  • @Praveen:当这个问题第一次出现时,我已经尝试过了。

标签: xcode ios6 linker-errors objective-c++ mach-o


【解决方案1】:

这真的是非常基本的初学者东西,这表明你不了解 C 系列语言的“头文件”和“实现文件”之间的区别。

您是否搜索过您的项目,但没有注意到您在头文件中声明变量但没有extern?您甚至在链接器错误中有全局变量的名称。是的,全局变量作为符号发送到目标文件 (.o) 中,如果您在三个文件中包含头文件并且所有三个文件都声明相同的全局变量,则会出现此错误。正是为了这个问题,发明了强大而美妙的extern关键字。

在头文件的上下文中,您不应声明实际变量,而应仅声明对它们的 extern 引用。这是您在 C、C++ 或 Objective C 编程的第一天就应该学习的东西。但是,您可以将代码放入头文件中,但是,包含该头文件的每个文件都会生成一组重复的符号,从而导致链接错误,并进一步加快全球变暖的步伐,并导致冰山融化。 (最后一部分开个玩笑。)

这是你的错误,这是一个头文件,但你把它当作一个实现文件来对待,并且声明的不是外部变量声明而是实际发出的全局变量到对象文件声明:

#import <Foundation/Foundation.h>

UIImage *SavedImage;
UIImage *SavedImage1;

NSString * vvvalue;
//UILabel * l2;
NSString * lll;

@interface NSObject (ShareClass)

@end

你现在告诉我。关键字extern 应该出现在上面的代码示例中的什么位置?变量的真正“将此变量发送到目标文件”声明在哪里?在头文件中?不。在实现文件中(.m 或 .c 或 .cpp 视情况而定)。

其次,添加 extern 关键字后,您现在遇到了第二个问题。您可能需要在相应的.m 文件中添加该全局变量的真实声明。是的,没错,您必须在 C 系列语言程序中“声明”每个全局两次。如果您确实需要系统中的所有模块能够看到该全局变量,则一次作为头文件中的 extern,一次没有 extern 关键字,仅在您的一个 .m 文件中。

当然,您确实知道要避免这种“全局变量混乱”,并且是糟糕的编程习惯对吗?您应该考虑将全局变量保留在代码之外,并尽可能将变量范围限制在单个实现模块中。

在 .h 文件中:

extern UIImage * myImage;

在 .m 文件中:

UIImage *myImage;

请阅读您的书,了解extern 关键字的含义。另外,我怀疑您可能打算这样做:

@interface NSObject (ShareClass)
  UIImage * InstanceVariable;  
@end

上面是一个实例变量(属于类的每个实例)。如果您需要将一些东西分组并将其放在一个对象中而不是使其成为全局变量,这是很常见的。

【讨论】:

  • 嗨,warren..非常感谢您的回答。是的……我只是一个新手,这导致了这些新手问题。我已经想出了解决方案,一天后我发布了这个问题。但我不知道变量应该像你提到的那样声明两次。不管怎样,谢谢你的信息。
  • 祝你好运,尽情享受。我推荐你买这本书:objective-C,Stephen Kochan 着,第五版。 amazon.com/Programming-Objective-C-Edition-Developers-Library/…
  • 嗯,一定会读完这本书的。再次感谢..!!
  • 这些问题不太可能得到答案,因为它们的范围太窄而且只针对您。如果您想要答案,请以对其他人更开放的方式提出问题。 (我该如何做一般主题 X,而不是如何解决这个定义非常狭窄的问题,除了我,宇宙中没有其他人会遇到。)
猜你喜欢
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 2012-12-31
  • 2017-11-14
相关资源
最近更新 更多