简体   繁体   中英

What is the best way to port from Objective-C to C++?

I have no Objective-C experience whatsoever but have a strong C++ background. Is there an automated tool/script or, worst case, some manual method using some excellent reference to port code written in Objective-C to C++? What are the difficulties involved?

Edit: I'm told the code uses Objective-C fairly trivially. It's an iPhone app that probably doesn't use much in the way of OS-level UI. The C++ version is meant for a non-Apple platform where GNUStep is not an option, so Objective-C++ is not an option.

I worked on same problem. And have some solutions:

  • Microsoft now offers its own Objective C "bridge", albeit only for UWP apps.

    https://github.com/Microsoft/WinObjC

  • In 2007 I tried to support Objective-C in Visual Studio and wrote my own ObjC runtime, but I do not have more time for writing parser for it. If you have, you can:)

    http://code.google.com/p/qobjc/

  • I wrote the basic functionality of Foundation Framework in C++. In this case, you need to port manually; your code will be on C++ but it will be like on Objective-C. This library worked on iPhone.

    http://code.google.com/p/dcocoa/

There are no automated tools that I'm aware of. The dynamic nature of Objective-C is very hard to translate to C++, so quite a bit of brain effort is going to be required for all but trivial Objective-C code. Are you willing to stay on OS X (or keep the GNUStep dependency if you're one of the few people using Objective-C on an OS besides OS X)? If so, the easiest approach is to use Objective-C++ to build a bridge beteen the Objective-C and C++ code. Objective-C++ is an Apple extension to Objective-C and the GCC compiler that allows you to mix Objective-C and C++ code. You can thus create Objective-C objects that call or reference (but not inherit from) C++ objects and you can send Objective-C messages to Objective-C instances from within C++ code.

Objective-C was just a pre-processor (and a runtime library). You'll find more problems in porting all the libraries than the end-user code. Nil-eating behavior is going to be fun when translating.

You are likely to start seeing bad design in all c++ code after using Objective-C frameworks for a while.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM