【发布时间】: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。
【问题讨论】:
-
如果这些都不起作用,我建议您尽可能上传源代码进行测试。