Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 6.2其他文件中函数的OSX未定义符号_Xcode_Macos - Fatal编程技术网

Xcode 6.2其他文件中函数的OSX未定义符号

Xcode 6.2其他文件中函数的OSX未定义符号,xcode,macos,Xcode,Macos,我相信这完全是一个努比问题,但如果有人能向我解释出哪里出了问题,我将非常感激 我在XCode中创建了一个新的Cocoa应用程序。称之为LinkerTest。这个基本的应用程序将构建并运行,将弹出一个简单的空白窗口 使用C++文件模板添加一个新的.CPP文件。称之为Test.cpp。这也创建了Test.h 在Test.cpp中添加一个简单函数: int TestMe(void) { return 1; } 在Test.h中声明我的函数 int TestMe(void); 在我的AppD

我相信这完全是一个努比问题,但如果有人能向我解释出哪里出了问题,我将非常感激

我在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"
在ApplicationIDFinishLaunching方法中添加:

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看看这个问题,它看起来很相似,但不是我的问题。我可以确认我的test.cpp列在编译源代码的构建阶段下


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应该被链接

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

如果这些都不起作用,我建议您在可能的情况下上传源代码进行测试。