Utilizing C++ in iOS and Mac OS X applications - c++

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++

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.

Objective C to C++ conversion [duplicate]

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.

OpenGL ES App on iPad/X-Platform: C++ or Objective-C?

I hope this has a technical (rather than a religious) answer, but I wonder if I should use Objective-C or C++ to create an OpenGL ES Application on the iPad?
As the ultimate goal would be to compile it on Windows and OS X as well, I'm inclined to use C++ (and only use ObjC for the stuff that I have to: App Delegate etc.), but I have zero experience with Objective-C Development.
Can I interact with C++ Classes from Objective-C? Is there any compelling reason to use ObjC over C++?
Or should I just use plain old C?
You can, it's called Objective-C++ and all you need to do is ensure your source files have a .mm extension for them to be treated as mixed Objective-C/C++ source files.
That said, generally speaking if you're aim is cross platform GLES support, try and write it in C++ for the common things, and anything else, Objective-C (where you need to do things like set up your EAGLLayer etc.). This is likely to work out the best for your purposes, as I presume you're already familiar with C++.
You need Objective-C to access touches and some other libraries that apple provides. Use .mm to make it Objective-C++ therefore you can use both.
But for portability, use Obj-C only when you need to. Also, what I would like to mention is that OpenGL ES is for Embedded Systems not for Mac OS X or Windows you need to use a OpenGL and yes they are different.
C++ and Objective-C can intermingle (as Objective-C++), though C++ classes cannot inherit from Objective-C classes, and vice versa. You must also take care to set a compiler flag in Xcode so that your C++ constructors and destructors are called as needed by Objective-C objects.
As to which you should use: either one will work. Objective-C will be necessary for, at least, the user-facing part of the app, as you need it to create a window, OpenGL context, etc. But your logic and engine can be in C++ or even ARM assembly, if needed.
The book iPhone 3D Programming might interest you. The philosophy of the book is to use Objective-C as little as possible. All the rendering code is written in portable C++.

Language Mixing: Model and View

Consider developing an application where the model will be written in C++ (with Boost), and the view will be written in Objective-C++ (with Cocoa Touch).
Where are some examples showing how to integrate C++ and Objective-C++ for developing iPhone applications?
Take it straight from the source: Apple has documentation on using C++ With Objective-C.
There really isn't much more to it besides, in my opinion, trying to keep the C++ and Objective-C parts as cleanly seperated as possible.
In your case it comes natural:
limit definition of C++ classes et al to the C++ model
restrict the Objective-C part to the view related code and using the C++ model
I don't know of any actual simple examples, but any cross-platform project that has a native GUI on the mac uses the same approach. One mostly clean example would be the Chromium source.

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.