【问题标题】:Xcode 6.2 OSX Undefined symbols for functions in other filesXcode 6.2 OSX 其他文件中函数的未定义符号
【发布时间】:2015-04-11 05:05:59
【问题描述】:

我敢肯定,这完全是个问题,但如果有人能向我解释出了什么问题,我将不胜感激。

我在 XCode 中创建了一个新的 Cocoa 应用程序。称它为 LinkerTest。这个基本的应用程序将构建并运行,并打开一个简单的空白窗口。

使用 C++ 文件模板添加一个新的 .cpp 文件。称它为Test.cpp。这也会创建 Test.h。

在Test.cpp中添加一个简单的函数:

int TestMe(void)
{
    return 1;
}

在 Test.h 中声明我的函数

int TestMe(void);

在我的 AppDelegate.m(创建应用程序时自动创建)中添加

#include "Test.h"

在applicationDidFinishLaunching方法中添加:

printf("Test = %d\n", TestMe());

现在尝试构建。一切编译正常,但无法链接。这是链接器命令:

Ld Build/Products/Debug/LinkerTest.app/Contents/MacOS/LinkerTest normal x86_64
cd /Users/chip/LinkerTest
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/chip/LinkerTest/Build/Products/Debug -F/Users/chip/LinkerTest/Build/Products/Debug -filelist /Users/chip/LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64/LinkerTest.LinkFileList -Xlinker -rpath -Xlinker @executable_path/../Frameworks -mmacosx-version-min=10.10 -stdlib=libc++ -fobjc-arc -fobjc-link-runtime -Xlinker -dependency_info -Xlinker /Users/chip/LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64/LinkerTest_dependency_info.dat -o /Users/chip/LinkerTest/Build/Products/Debug/LinkerTest.app/Contents/MacOS/LinkerTest

这是我得到的错误:

Undefined symbols for architecture x86_64:
    "_TestMe", referenced from:
       -[AppDelegate applicationDidFinishLaunching:] in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

所以知道我做错了什么吗?在我看来,添加它也应该链接的文件,但它没有。我知道文件 Test.cpp 会被编译,如果我在文件中添加垃圾,那么编译器会立即对垃圾进行攻击。

编辑:还有几件事。

1) 看看这个 stackoverflow(Getting XCode to include, compile and link existing (C++) codebase in XCode 4.3(.1)) 问题,它看起来很相似,但不是我的问题。我可以确认我的 test.cpp 列在我的编译源中的 Build Phases 下。

2) 查看 LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64 文件夹,我发现 Test.o、Test.d 和 Test.dia 向我建议 Test .cpp 实际上正在编译。在该文件夹中,我还找到了 LinkerTest.LinkFileList,当使用文本编辑器打开它时显示应该链接 Test.o。

【问题讨论】:

  • 如果这些都不起作用,我建议您尽可能上传源代码进行测试。

标签: xcode macos


【解决方案1】:

答案是,当您创建一个默认的 Cocoa 应用程序时,AppDelegate 文件是一个 .m 文件。将该文件更改为 AppDelegate.mm 可以解决问题。我不知情的猜测是 .m 文件只能处理 .c 文件,而要正确处理 .cpp 文件,您必须使用 .mm 文件。为什么 xcode 的默认 Cocoa 应用程序的默认文件不会是 .mm 文件,这超出了我的薪酬等级,但确实如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2012-08-29
    • 2023-03-30
    相关资源
    最近更新 更多