Objective C to C++ conversion [duplicate] - 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.

Related

Developing for iOS in C++ (with/without Xcode)

What are the methods of developing for iOS with C++? I am familiar with using Objective-C++ within Xcode, by selecting Obj-C as my language when creating a new project, but what else is there. I know there are tools like Xamarin that allow for iOS development in C#, however i'm curious as to what officially supported ways of developing for iOS in C++ there are? Can an iOS app be written completely in C++ with no Objective-C and compile?
I have an app that pretty freely intermingles Objective-C (.mm files), Objective-C++ (.m files), and C++ (.cpp) as needed. The C++ code is legacy functionality that implements the heavy lifting of our app and has worked across a number of platforms for 20+ years. No need to re-implement that.
To write a C++ object in XCode, just do it. Create .cpp and .h files like you would anywhere else.
When you need to interact with a C++ object from Objective-C, you can either go into the properties of the .m file in XCode and change it to use the Objective-C++ compiler, or (better yet) just name it .mm instead of .m. Then you can include your C++ header files with standard #include statements, create C++ objects with new, delete them with delete, and access members with dot notation and pointer notation just like you would from C++.
Objective-C is a lot easier to use when dealing with the UI and system calls. It isn't hard to learn. There are probably ways to access all of those objects and methods from C++, but the gymnastics you would have to do each time might make it easier to simply learn enough Objective-C (or Swift) to do those parts in the language Apple intended. (Keep in mind that to figure out how to do everything in C++ while looking at sample code and documentation that assumes Objective-C or Swift, you're going to have to master those languages anyway.)
Yes, you can even go to extreme lengths to avoiding Objective-C, you won't be able to do so completely since UIKit depends on it, but nearly every other framework has a C API. Take a look at this question and the answer to it How to write iOS app purely in C
C is not C++, but the answer will give you a hint on what you can do. It will involve including the objc runtime, and using objc_msgsend.

Utilizing C++ in iOS and Mac OS X applications

I'm somewhat moderate in my C++ knowledge but know next to nothing regarding Objective-C. I am planning (and even starting to) learn Objective-C so I can attempt to write a few iOS and even Mac OS X applications but I'm very curious about something I haven't been able to find yet.
Is it possible to write an iOS and / or a Mac OS X application entirely with C++ including the UI components? If it isn't possible to do it entirely in C++ then to what degree is it possible to use mostly C++?
I haven't seen any examples that demonstrate either of these items. I am planning on writing a few mobile applications for iOS and Android and, where possible, I'd like to contain most of my logic inside of C++ code for maximum portability. I already know I can do this but I am unsure of the degree.
Short answer, yes.
For pure C++, you should take a look at the QT framework.
Otherwise, you'll have hard time for the UI part.
But also remember that Objective-C can be mixed with C++.
That's called Objective-C++ (.mm files).
You can then write code that mix C++ and Objective-C code.
With this, you can have the UI parts in Objective-C (may be better, as it will use the native frameworks for the UI components), and the other things in C++.
If you've decided to learn Objective-C, but still want to code in C++ for some parts, I would recommend this approach, instead of pure C++.
On iOS, this is also the only way. While you can code in C++, you have to use Objective-C for the UI part.
EDIT
Here are a few links to get started with Objective-C++:
Strategies for Using C++ in Objective-C Projects
From C++ to Objective-C
CocoaDev
Look this question
I don't know about Mac OS, but in IOS applications you can use C++ in logic but you have to write user interface on Objective-C.
Is it possible to write an iOS and / or a Mac OS X application
entirely with C++ including the UI components? If it isn't possible to
do it entirely in C++ then to what degree is it possible to use mostly
C++?
I think it is possible to use pure c++ if you want to depend on some 3rd part lib, then yes.
Without that 3rd part lib you have to write all the UI using objective-c the rest you can use c++ as much as you like.
Changes required in build settings to use C++ files in iOS project
under "Apple LLVM compiler 4.2 - Language" Option
C++ Language Dialect: Compiler Default
C++ Standard Library: Compiler Default
You could use Cocos2d-x engine and write pure C++ for your iOS app, including UI (cocos has basic ui classes like buttons, scroll views, table views, etc - but you need to develop your own Controllers, and Models using various C++ libraries like Hiberlite, or raw SQLite etc)
Cocos has its own interface with iOS/MacOS (a few basic .mm objective-c classes to setup Application run cycle - like app delegate, or runloop and drawing cycle)
So you just write your user code with C++

What is the difference between c++, objective-c and objective-c++?

I want to know the difference between c++ and objective-c and objective-c++.
Can any one give me the difference and Can we use the c++ for iPhone development
Thank you,
Madan Mohan
C++ is Bjarne Stroustroup's language based on adding classes and metaprogramming to C in such a way that puts most additional work into the compiler, and relies on least possible effort at runtime.
Objective-C is Brad Cox's language based on adding a SmallTalk-style dynamic message-passing runtime library to C, with a small amount of syntax addition to make it easier to use.
Objective-C++ is, to put it bluntly, what you get when you add the Objective-C runtime and syntax to C++. It has its limitations (e.g. you can't create an Objective-C subclass of a C++ class or vice versa, and Objective-C doesn't like C++ namespaces) but allows you to use C++ classes from Objective-C objects and vice versa.
You can use Objective-C++ in iPhone development. What this means practically is that you could write an application whose object model was entirely C++, where the controller layer would need to interface to Objective-C in order to use the Cocoa Touch APIs.
C++ and Objective C were/are two different approaches to adding object orientation to C. Current objective C compilers also accept C++ as input, so you can build a program with some files written in Objective-C and other files written in C++. When C++ is used this way, it's often called Objective-C++.
1) C++ is a language derived from C that adds Object Orientation (OO) amongst other features. *
2) Objective-C is a language derived from C that adds Object Orientation (OO) amongst other features. *
3) Objective-C++ is Objective-C that you can use C++ classes with.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
(*) Though they both try to solve the same problem they do it quite differently. There is some information about the differences on wikipedia and I'm sure you can use google to find more.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
This will very likely give you code and possibly memory bloat. As you know, iOS programming should be a lean as possible: minimize both the library size and runtime memory needs. iOS programming and runtime environments are also highly optimized for Objective C.
Pure ObjC is much better than C++ for iOS. Unless you're trying to use existing large C++ code base it will probably be better to re-write from scratch. Nearly all of the C++ STL have analogs in the iOS frameworks, often easier to use, and highly optimized by Apple.
Learn Objective C memory management, get familiar with the frameworks and go for it.

C vs C++ (Objective-C vs Objective-C++) for iPhone

I would like to create a portable library for iPhone, that also could be used for other platforms.
My question is the fallowing:
Does anyone knows what is the best to be used on the iPhone: Objective-C or Objective-C++? Does it works with C++ the same way as Objective-C with C or not?
Reasons: Objective-C is a superset of C, but Objective-C++ is not a superset of C++.
Thanks in advance!
UPDATE: What about memory usage, speed in the same implementation of an use case?
UPDATE1: If anyone can provide any more information, he'll be welcome.
If you're writing a portable library, and don't need specific language features for it, why not write it in straight C? It can be easily called from C++, C, Objective-C, and many, many other languages.
If you require specific language features that aren't available in C, you should tell us what they are so you can get better advice =)
I believe your information is incorrect. Objective-C++ is a superset of C++. Any C++ is legal Objective-C++. In addition, you can mix Objective-C and C++ code in an Objective-C++ (usually .mm) file and (with some restrictions) mix Objective-C and C++ class instance variables within an Objective-C++ class. Objective-C++ is particularly useful for interfacing between Objective-C and a C++ library. Write your cross-platform library in C++. You can then call it from Objective-C++ within an application. Re-read the Objective-C++ section of the Objective-C language guide for more info.
The major downside to using Objective-C++ is increased compile times (Objective-C++ is even worse than C++ and the Clang LLVM compiler doesn't handle Objective-C++ yet). There is no performance difference between Objective-C and Objective-C++ beyond any differences in the code that's called (e.g. if you're a better C++ dev., your C++ will probably be more efficient than your C library and visa versa).
They're not really different languages. Objective-C++ is just Objective-C with slightly limited support for including C++ code. Objective-C is the standard dialect, but if you need to work with C++, there's no reason not to use it. AFAIK, the biggest practical difference (aside from allowing use of different libraries) is that Objective-C++ seems to compile a bit slower. Just be sure to read up on it first if you do decide to go that route, because the merging of C++ and Objective-C is not 100% seamless.
If you are going to use this library on platforms other than the iPhone and Mac OS, you should use C++. If you plan only to port it to Mac OS, then go ahead and use Objective-C. Objective-C++ is just a way to integrate existing C++ code into Objective-C projects. Thus, you would not write a library in it. You might, however, use Objective-C++ to use this library from an iPhone or Mac project.
The truth is you can use C or C++ in Xcode. The reason to use Objective C for Cocoa and Cocoa Touch (iphone) is because of it's ease of use and how it handles many of the memory issues you would come across on it's own. Objective C and Xcode work like a dream. I would also recommend checking out http://www.kevincallahan.org/software/accessorizer.html
Accessorizer is amazing and cuts development time into nothing.
Having said that I see no problem in writing functions/methods in C or C++ and using them within a part of your XCode project. Just do not expect to use Objective C to play as nicely on other platforms. Apple and Windows development IDE's are different and Microsoft has a nice habit for not making things as compatible with other environments (not just Apple.)
I am glad to hear someone out their trying to reuse their code for other projects : ) Too many people forget to create their own libraries these days and I haven't the slightest idea why.
I hope this helps. Cheers,
Matthew
Here is your caveat: Spend the hundred on becoming a developer and launch xcode the IDE you will be working with on the Mac - this would be the best way for you to decide as it is apparent that you do not fully understand how this platform works at all.
Please don't be rude to everyone trying to help you. This community is built on respect. The people here really actually do help out and care and there are few places like this on the net left.
Go for C or C++ - I do not see any reason for recommending one over the other without knowing more about your goals.
In terms of memory usage, C++ and C do demand more disciplined coding. In terms of speed, C and C++ will in virtually any case be faster than any ObjC/ObjC++ solution. I do frequently mix my Objective C applications with C and C++ code and was always pleased with the results. As Jonathan correctly stated, ObjectiveC++ is mostly a bridge for using C++ code/libraries within your Objective C application/s.
You can freely use C++ objects from within Objective C++. You can however NOT use Objective C/C++ objects within C++. So its a one-way-road. To overcome any limitations imposed by this restriction, simply wrap your C++ objects with Objective C++ classes where needed.
"Objective-C is a strict superset of C, and Objective-C++ is a strict superset of C++." - https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html

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.