For an upcoming XCode iOS project I'm planning on writing my views and view controllers in Objective-C. My model will be provided to me in C++.

I am aware of 2 ways of using C++ in an objective-c project. They are described below. What are the pros and cons of each method. Is there any other recommended way, and what are its pros and cons?

-In C++ header files, use extern "C" declaration as described in Wikipedia.

-Wrapping C++ with an Objective-C++ as described by Rob Napier.


Your views and view controllers will need to be Objective-C, because of the frameworks. This much is given. The question then is where to put the bridge between your C++ code and your Objective-C code.

Firstly, I'd advise against wrapping the C++ code in C functions with an extern C {} linker convention. You've then given yourself two problems: how to handle the C++ classes and types from within C, and how to use the structured C interface from within Objective-C objects. Both of these problems are surmountable but you may as well not have to introduce them.

Three approaches seem possible:

  • write your application's logic and behaviour in C++, where certain of the classes have member fields that are Objective-C types so that you can interface with the frameworks. This approach is likely to run into "impedance mismatch" problems between the frameworks - which have a "don't call us, we'll call you" policy - and your own code which is trying to take control of the application's behaviour from outside the framework's ambit.

  • in your view controllers, keep references to C++ types as instance variables. Your view controller classes will be Objective-C++ files.

  • write Adapter or Façade classes in Objective-C++ that provide an Objective-C API and internally manage the C++ types. Your view controllers access the model exclusively via this layer.

Of these approaches, I would prefer the third. You separate the responsibilities of "managing the model" (in the C++ code), "displaying model content" (in the ObjC view controller classes) and "bridging C++ to ObjC" (in the adapter layer). You'll meet many developers who know ObjC but not C++ and vice versa, this approach makes it easiest for those developers to contribute to your codebase, whether they end up working with you or just trying to help you out on the web.


I don't know much about Objective-C, but the extern "C" approach will only work if your C++ header files don't contain C++ classes, only C style structs and functions. If your model is written in C++, I guess this will not be the case, so I think you will have to try the second road.

extern "C" is intended to use C code in C++, not vice versa.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.