Apple Mach-O 链接器 (Id) 错误:调用 c++ 时.m 文件中的文件

发布于 2024-12-01 01:42:59 字数 651 浏览 5 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2024-12-08 01:42:59

如果要在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文