29

I wanna try to write and compile C++ code for iOS and Android-devices. I've been using Xcode for some time (I was writing c++ code for MacOS and Windows). So I wanna use Xcode for my purposes.

The problem is that I don't know how to compile C++ code both for Android devices and for iPad/IPhone. I wanna use crossplatform-solution for iOS and Android but don't want to use such frameworks as Marmalade (c++) or others.

Is it real or not? :) Is there tutorials or other helpful materials?

Cœur
  • 37,241
  • 25
  • 195
  • 267
JavaRunner
  • 2,455
  • 5
  • 38
  • 52
  • 1
    Did you try doing a google search, first? – crashmstr Mar 04 '13 at 21:20
  • I don't know about iOS, but Android is essentially java, and so can use C++ code through JNI. – Motomotes Mar 04 '13 at 21:22
  • 1
    If you don't mind c#, you could use Xamarin http://xamarin.com/monotouch – ahodder Mar 04 '13 at 21:23
  • @Motes: You can use the NDK and NativeActivity and never touch JNI for Android development. – Cornstalks Mar 04 '13 at 21:24
  • 1
    @Motes There is the [Android NDK](http://developer.android.com/tools/sdk/ndk/index.html) – Mike D Mar 04 '13 at 21:25
  • 4
    Yes, you can use C or C++ code in an Android and/or iOS project. For example, both Box2D (C++) and Chipmunk (C) physics engines are 100% compatible with both iOS and Android, and many 2D popular games use these physics engines. – Cornstalks Mar 04 '13 at 21:27
  • @crashmstr: sure, I was using Google search for getting something about this subject but I didn't find any simple and clear tutorials. – JavaRunner Mar 04 '13 at 21:40

1 Answers1

65

You can most definitely use C++ on iOS and Android. I have written my own game engines for both.

Xcode is the normal IDE for iOS and natively supports "Objective-C++" development (The compiler supports both Objective-C and C++). You will need to write a thin wrapper in Objective-C to interact with the OS, but you can write everything else in C++ if you like. If you download the Xcode command line tools you can also build from scripts.

Eclipse is the usual IDE for Android and if you download the NDK you can utilize JNI to interface between Java and C++. Much like iOS you will at least require a Java wrapper to interact with the OS.

You are not limited to Eclipse and Xcode, however. You could for example use emacs and/or employ CMake to allow you to try and consolidate the project settings a bit. Emacs is a very powerful and flexible IDE, but it has a very steep learning curve and not very straight forward to install. If you do this on Mac I would recommend emacsformacosx and installing ECB and autocomplete as well. CMake is basically a cross platform build system that allows you to build many different project files (Visual Studio, Xcode, Eclipse, etc.) from one source tree. Much like emacs this is super powerful yet somewhat of a steep learning curve. If you want to jump in right away you will likely want to start off with both Xcode and Eclipse. If you are developing on a PC you would require a Mac VM or a Hackintosh to use Xcode and cygwin to use the NDK for android (Macbook Pro users can have fun right away with little hassle).

Native Android development is definitely much more difficult. Not only do you have much more device segmentation to contend with, but native debugging is not very straight forward. You must run a GDB server on the device and connect with it to set breakpoints and many people do this from the command line. I have heard some people have success with the Tegra Tools for Eclipse if you want a GUI based IDE.

I would definitely recommend developing on iOS first as you have way more powerful tools at your disposal from the IDE. You can track memory leaks, profile code, set break points easily ;), etc. Once you develop a feature on the iOS you can then wrestle with the .mk files and other goodness of Android native development.

Here is some information on the core objects and life cycle for iOS:

and for Android:

As long as you play nice with the OS and respect the lifecycle of the application you are free to make calls to C++ logic. You can do this directly with iOS, but with Android you have to marshall the calls through JNI. If you encapsulate the platform specifics you can share the C++ logic between both platforms.

I'm a huge advocate of C++, but the choice to use it does depend on the situation for these platforms. C++ is not the popular choice for these platforms and as such you will not find as much documentation (your on your own much of the time). The complexity can go up a bit if you are not very familiar with C++ (Are you familiar with RAII, design patters, STL?).

Here are a few situations where it may make sense:

  • If you need to utilize a library that was authored in C/C++
  • If you would like to build your own platform agnostic framework for your apps or games
  • If you are working with realtime / performance critical applications such as games

I'm not saying you MUST use C++ in these situations as many people have developed games in Objective-C. I'm just saying it may be something to consider if you need to get the most out of the hardware or you simply want something more portable.

Matthew Sanders
  • 4,875
  • 26
  • 45
  • Very intresting! Don't you have some tutorials about what you are talking about in this post? :) – JavaRunner Mar 04 '13 at 21:42
  • 1
    This topic is pretty near and dear to my heart as I work in the game industry and also hobby with it on my spare time. I will definitely try to expand this post a bit more to provide clarity. – Matthew Sanders Mar 04 '13 at 21:47
  • It would be pretty cool! :) I've never developed for iOS but I have some experience of developing for Android (with Eclipse). But now I wanna try to "immigrate" to C++ and to XCode. And I wanna build my own game framework (easy but fast :) ). – JavaRunner Mar 04 '13 at 21:54
  • Have you checked out the Juce C++ framework? It's very cross-platform. However I've found myself to be more productive with the UI side using native tools, for Android at least and I assume would be similar or more so with iOS. Great explanation and example here: http://stackoverflow.com/questions/18334547/how-to-use-the-same-c-code-for-android-and-ios – Adamski Nov 16 '14 at 14:57
  • Can we use Eclipse CDT for that purpose? Do we have to change the compilers in order to target both platforms? thank you – LolaRun Dec 09 '15 at 18:13
  • 1
    You can (sorta) use any IDE you want. You will likely need to still compile the project with XCode, however, to target iOS. I was looking for a nice example of cross compiling with CMake to iOS long ago, but it wasn't very fruitful back then. A quick google turned up [this](http://stackoverflow.com/questions/12630970/compiling-for-ios-with-cmake). You can try to search for things like "Cross Compile C++ to iOS". Then you would also need to run [code signing](https://developer.apple.com/support/code-signing/). – Matthew Sanders Dec 09 '15 at 18:50