混合 Objective-C 和 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2710507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:26:37  来源:igfitidea点击:

Mixing Objective-C and C++

c++objective-cobjective-c++

提问by LandonSchropp

I'm trying to mix Objective-C with C++. When I compile the code, I get several errors.

我正在尝试将 Objective-C 与 C++ 混合使用。当我编译代码时,我收到了几个错误。

A.h

#import <Cocoa/Cocoa.h>
#include "B.h"

@interface A : NSView {
    B *b;
}

-(void) setB: (B *) theB;

@end

A.m

#import "A.h"

@implementation A

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
}

-(void) setB: (B *) theB {
    b = theB;
}

@end

B.h

#include <iostream>

class B {

    B() {
        std::cout << "Hello from C++";
    }

};

Here are the errors:

以下是错误:

/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: error: iostream: No such file or directory
/Users/helixed/Desktop/Example/B.h:3:0 /Users/helixed/Desktop/Example/B.h:3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'B'
/Users/helixed/Desktop/Example/A.h:5:0 /Users/helixed/Desktop/Example/A.h:5: error: expected specifier-qualifier-list before 'B'
/Users/helixed/Desktop/Example/A.h:8:0 /Users/helixed/Desktop/Example/A.h:8: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:26:0 /Users/helixed/Desktop/Example/A.m:26: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:27:0 /Users/helixed/Desktop/Example/A.m:27: error: 'b' undeclared (first use in this function)

回答by Pablo Santa Cruz

You need to name your .mfiles .mm. And you will be able to compile C++ code with Objective-C.

您需要将.m文件命名为 .mm。您将能够使用 Objective-C 编译 C++ 代码。

So, following your example, your AView.mfile should be named AView.mm. It's simple as that. It works very well. I use a lot of std containers (std::vector, std::queue, etc) and legacy C++ code in iPhone projects without any complications.

因此,按照您的示例,您的AView.m文件应命名为AView.mm。就这么简单。它运作良好。我在 iPhone 项目中使用了很多 std 容器(std::vector、std::queue 等)和遗留的 C++ 代码,没有任何复杂性。

回答by LandonSchropp

Never mind, I feel stupid. All you have to do is rename AView.m to AView.mm so the compiler knows it's Objective-C++, and it compiles without a problem.

没关系,我觉得自己很傻。您所要做的就是将 AView.m 重命名为 AView.mm,这样编译器就知道它是 Objective-C++,并且编译没有问题。

回答by Leonardo

you could keep the interface cleaner with forward declaration of C++ classes:

您可以通过 C++ 类的前向声明来保持界面清洁:

#import <AnObjCclass.h>
class DBManager; // This is a C++ class. NOTE: not @class

@interface AppDelegate : UIResponder <UIApplicationDelegate,
                                    CLLocationManagerDelegate,
                                    MFMailComposeViewControllerDelegate>
{
    DBManager* db;
...
}

回答by Govind

I am sharing some of the points that I understood on this topic.

我正在分享我对这个主题的理解的一些观点。

We can mix both .cpp and .m files with a pure C interface. As we know the Clang compiler will support C++, Objective C as well as Objective C++, it might be a better means for mixing these languages.

我们可以将 .cpp 和 .m 文件与纯 C 接口混合使用。正如我们所知,Clang 编译器将支持 C++、Objective C 以及 Objective C++,它可能是混合这些语言的更好方法。

One thing when mixing these languages to be taken care is using the header files. We can keep the C++ out of our Objective C headers by declaring the Cpp objects in class extensions.

混合这些语言时要注意的一件事是使用头文件。通过在类扩展中声明 Cpp 对象,我们可以将 C++ 排除在我们的 Objective C 头文件之外。

Alternatively we can declare the cpp objects just at the start of @implementation block in our Objective Cpp(.mm) file.

或者,我们可以在目标 Cpp(.mm) 文件中的 @implementation 块的开头声明 cpp 对象。

Managing the memory will be a concern when we are dealing with Cpp objects. We can allocate memmory for an object using ‘new' and release the memory by calling ‘delete object'. Normally if we are using ARC, we need not be aware of releasing the memory for an object.

当我们处理 Cpp 对象时,管理内存将是一个问题。我们可以使用“new”为对象分配内存,并通过调用“delete object”释放内存。通常,如果我们使用 ARC,我们不需要知道释放对象的内存。

While using the cpp classes, we can declare a Cpp object in two ways say CppWrapper wrapper and CppWrapper *wrapper where CppWrapper is a Cpp class. When we are using the latter the programmer is responsible for managing the memmory.

在使用 cpp 类时,我们可以通过两种方式声明 Cpp 对象,即 CppWrapper 包装器和 CppWrapper *wrapper,其中 CppWrapper 是 Cpp 类。当我们使用后者时,程序员负责管理内存。

Another main thing is that when we are calling an objective C method with parameters, we are passing the references, while in cpp we need to pass the parameters by reference by using the ‘&' keyword, otherwise copy of the object is been passed.

另一个主要的事情是,当我们调用带有参数的客观C方法时,我们传递的是引用,而在cpp中我们需要使用'&'关键字通过引用传递参数,否则传递对象的副本。

Deallocation of Objective C object is handled at runtime, where when ‘delete' is invoked to an Cpp object, it will no longer remains in the memory.

Objective C 对象的释放是在运行时处理的,当对 Cpp 对象调用“删除”时,它将不再保留在内存中。

When writing Cpp, we have shared pointer and weak pointers which is similar to the strong and weak in Objective C.

在编写 Cpp 时,我们有共享指针和弱指针,类似于 Objective C 中的强指针和弱指针。

http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++http://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1

http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++ http://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1

回答by Gabe Rainbow

In situations where you wish to introduce a simple C++ function like std::cout <<then Hot Licks offers a good alternative.

在您希望引入一个简单的 C++ 函数的情况下,std::cout <<Hot Licks 提供了一个很好的选择。

Change the "Identity and Type" From: Objective-C sourceTo: Objective-C++ source

将“ Identity and Type”从:更改Objective-C source为:Objective-C++ source

The .mm extension simply identifies the file type; and then you are seeking a Objective-C++ not a Objective-C type.

.mm 扩展名只是标识文件类型;然后你正在寻找一个 Objective-C++ 而不是 Objective-C 类型。