Compiling a C++ program to an IOS application

Discussion in 'iOS Programming' started by richi27, Aug 2, 2012.

  1. richi27 macrumors newbie

    Aug 2, 2012
    Good day to everyone. I register to this forum to seek for help. I've already spend a lot of time googling but haven't been lucky, or maybe haven't used the correct words because I am pretty sure that my task is not something new.

    I hope someone can help me.

    What I want to do is compile a simple Hello World C++ program into an Iphone application.

    that's it, very straight forward. You may wonder why not just write that simple application. Basically I want to understand the process of taking advantage of a c++ or c program or application and implement it in an IOS application.

    I am working on a mac os 10.6.2 therefore I have xcode 3.2.6.

    Any help on this will be greatly appreciated.
  2. 1458279 Suspended


    May 1, 2010
    I run Xcode 4 so I don't know how well this applies:

    Xcode and the LLVM complier support C++, the newer the versions, the more they support, so your support is probably be less.

    Several of the example projects that Apple has in their documentation, have .mm files They are C++ code compiled and linked into the app.

    Right now, I'm looking through the avTouch app.

    Open Xcode, click on Organizer, search for avTouch, open AudioViews, look at files with '.mm'

    I know you asked about 'hello world' I don't know exactly how an app would be developed entirely in C++ or if it can be done, but this might point you in the right direction.
  3. samdev macrumors regular

    Sep 16, 2011
    You either compile a C++ library as a static library (.a) or mix C++ classes/functions with Objective-C code
    by renaming your files with the .mm extension.
  4. richi27 thread starter macrumors newbie

    Aug 2, 2012
    static library

    Hey samdev,

    I had heard about static library, do you know of any good tutorials?
  5. samdev macrumors regular

    Sep 16, 2011
    I don't know any offhand, only Google can tell.

    If you want some brief instructions on how to create a static library with XCode 4.3:

    1. In your iPhone project, click on the "Add Target" icon. It's a big icon found next to the "Validate Settings" icon.

    2. Choose the template iOS -> Framework & Library -> Cocoa Touch Static Library.

    3. Your new static library should now be listed under "Targets" in your Project window.
    Click on it, and goto Build Phases -> Compile Sources. This is where all of your C++ source files go.
    Add them here.

    4. Goto Build Settings -> Search Paths -> Header Search Paths. Add any paths to your C++ header files.

    5. It should be ready to compile, so build it. If everything goes well, you should see a static library
    file called "foo.a" in your project folder window.

    How to integrate it into your iPhone application:

    1. In your iPhone project, add "foo.a" to Build Phases -> Link Binary With Libraries.

    2. Any Objective-C source file (.m) that you include the C++ header files should be renamed to (.mm).

    3. Let's say your C++ library consists of a single C++ class called MyWorld.
    What I would do is dynamically allocate a single C++ instance of this class in the viewDidLoad method:

    myWorld = new MyWorld();

    Use this pointer everywhere to call any functions, pass/retrieve data, etc...

    4. When done, de-allocate it in the dealloc method:
    if(myWorld) {
    delete myWorld;
    myWorld = NULL;

    If you ever use the physics engine called Box2D, then this is exactly what you need to do
    to use it with your iPhone app. Box2D is a 2D physics engine written in C++.

Share This Page