2

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

架构 i386 的 20 个重复符号

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

问题是我已经检查了我的项目中是否存在任何重复的类文件、图片、音频文件(没有)。我已将所有 .m 文件导入到build phase-> compile sources。还通过二进制库链接导入正确的框架,并检查图片、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)
4

1 回答 1

7

这真的是非常基本的初学者东西,向我暗示你不理解 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

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

于 2013-04-23T15:22:33.067 回答