[英]OSX command line app linker error
我已经在Xcode 5中创建了OSX命令应用程序
这是main.m
#import <Foundation/Foundation.h>
#import "ConnectionListener.h"
#import "SOMatrix.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello, World!");
print_m();
}
return 0;
}
这是我的头文件:
#ifndef __GDC1__SOMatrix__
#define __GDC1__SOMatrix__
#ifdef __cplus
#include <iostream>
#endif
int print_m();
#endif /* defined(__GDC1__SOMatrix__) */
这是SOMatrix.mm文件的部分列表
#include "SOMatrix.h"
#include <iostream>
using namespace std;
int print_m() {
// logic removed to keep it short; no compile time error
return 0;
}
构建项目时,出现链接器错误:
Undefined symbols for architecture x86_64:
"_print_m", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不明白为什么将函数的showhow更改为名称('_print_m')前导下划线。
为什么会出现此错误? 我是否需要将.mm文件显式添加到项目中?
您需要更改以下行:
#ifdef __cplus
#include <iostream>
#endif
这在您的.h文件中:
#ifdef __cplusplus
#include <iostream>
extern "C"
{
#endif
与同伴:
#ifdef __cplusplus
}
#endif
在.h文件的末尾。
因为您试图从Objective-C访问C ++函数,所以C ++往往会做一些名称修饰(例如,添加下划线)。 添加“ extern "C"
“位使您的Objective-C代码可以找到您的C函数声明。 这个相关问题的答案可能比我能更好地阐述 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.