Apple Mach-O 链接器 (Id) 错误:调用 c++ 时.m 文件中的文件
我有一些 cpp 文件,其中编写了我的函数。我想在我的 iPhone 应用程序中调用该函数?我如何在我的 .m 文件中调用 C++ 方法???
这是我的 .cpp 文件
int add(int a, int b)
{
return a+b;
}
现在在我的 viewDidLoad 事件中我使用以下代码只是为了获取结果并显示在标签中。
NSNumber *n = [[NSNumber alloc] initWithInt:add(5,7)];
[myLabel setText:[NSString stringWithFormat:@"%@",n]];
编译时收到以下错误:
Undefined symbols for architecture i386:
"_add", referenced from:
-[usingc__ViewController viewDidLoad] in usingc__ViewController.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
代码中的错误在哪里?为此需要什么编辑?
I have some cpp file where my functions are written. I want to call that functions in my iPhone app? How can i call c++ methods in my .m file???
Here is my .cpp file
int add(int a, int b)
{
return a+b;
}
Now in my viewDidLoad event i am using following code just to fetch the result and display in a Label.
NSNumber *n = [[NSNumber alloc] initWithInt:add(5,7)];
[myLabel setText:[NSString stringWithFormat:@"%@",n]];
I received the following Error while compiling:
Undefined symbols for architecture i386:
"_add", referenced from:
-[usingc__ViewController viewDidLoad] in usingc__ViewController.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
Where is the error in the code?? What edit is necessary for this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在 Objective-C 文件中使用 C++ 文件,请将文件扩展名从 .m 更改为 .mm。
If you want to use C++ file in your Objective-C file, change the file extension from .m to .mm.