开发者

Objective C Project using C++ POSIX Classes

开发者 https://www.devze.com 2023-01-14 19:53 出处:网络
I have to create a iOS Programm using C开发者_运维问答ode of some C++ POSIX Classes. I already read the \"Using C++ With Objective-C\" manual of the Apple Developer Center.

I have to create a iOS Programm using C开发者_运维问答ode of some C++ POSIX Classes. I already read the "Using C++ With Objective-C" manual of the Apple Developer Center. They describe how to mix C++ & Objective C code in a .mm file.

My question is, is there any possibility to use the C++ Classes in my .h/.m files of a normal Objective C Project?

Or is it necessary to write the whole Project in that .mm file style with its own main?


You can combine in a project any types of files, say .c, .m, .cpp and .mm, and the compiler is chosen automatically depending on the extension. For example, you can keep the standard main.m file which comes with the XCode template, and add your new .h and .mm files to use Objective-C++.

In other words, there's no distinction between a normal Objective-C project and a Objective-C++ project. You just have to use .mm extension for the specific files which needs Objective-C++. This can be used in any project.


You have to force compilation of the Obj-C files in which you want to use C++ to Obj-C++ in the build menu. You can then create and use C++ objects in your Obj-C classes.


It depends on how you want to use the C++. In my projects I usually only make a few calls out to do some heavy lifting, etc.

What I do is have C++ in .cpp files, then create a few .mm files that have headers that have no C++ in them. These .mm files are obj-c wrappers for the C++. Then the C++ is 'contained' to the original posix files, plus a few files that give the C++ classes and calls all the interface you need. As few .mm files as possible is a good thing.

Keeping the C++ out of most of your code makes debugging, etc easier.

--Tom


Select Project directory in Xcode, In Build Settings tap, choose "Compile Sources As" is Objective-C++. No need change name type from .m to .mm.

0

精彩评论

暂无评论...
验证码 换一张
取 消