3 Cross-Platform UI Strategies for C++ Developers

Posted by on in Tutorial

C++ has had a well-earned reputation for being a powerful, high performance cross platform language. It has also enjoyed great success within embedded systems of all types as C++ is excellent with low level hardware and it is supported on virtually every single operating system. Why then hasn’t it become the de-facto programming language for mobile, cross-platform development?

 One of the key reasons is that the creators of Android and iOS selected different primary languages for their application frameworks (although both support C++ very well) but another important reason, in this world of touch centric apps, is that the C++ standard has never specified a user interface library.  So, what is a C++ developer to do? 

In this paper, I’ll discuss 3 user interface strategies for C++ developers doing cross platform development. I’ll discuss the pros and cons of each approach as perceived by most C++ developers as well as provide my take. In short, C++ developers targeting Android and iOS have to make a choice between rendering UIs with HTML, with the operating system platform controls, or with a 3rd party application framework.

Basic Architecture Considerations

With any of these UI rendering strategies, C++ developers should consider an architecture that separates UI logic from application logic (and any backend logic as well) depending on their use case.  Patterns such as Model-View-Controller (MVC), Model-View-Presenter (MVP), and Model-View / View-Model (MVVM) all present excellent approaches and you can find a concise explanation of the differences here. In the first two cases (HTML and Platform rendered), this type of approach is actually necessary since the UI and the application logic are typically written in different languages and frameworks, while in the 3rd case it is an optional consideration. Regardless, it is just good practice for modern apps.

HTML5 Widgets

Android and iOS both fully support embedded webkit applications. Essentially, your app bundle includes your C++ libraries, a JavaScript interpreter and an HTML 5 Canvas known as BrowserField (courtesy of WebKit). In this scenario, your user interface will be rendered by the embedded BrowserField using HTML 5 widgets, your event handling will call JavaScript functions, and your JavaScript will call into your C++ application logic.

The biggest advantage to this approach is that you can write a common user interface codebase as both Android and iOS support WebKit equally well. The biggest downside is the performance and look and feel of HTML 5 rendered user interfaces (they will look like web controls), not to mention the fact that you will also have to write JavaScript bindings for your C++ code. Another downside is you will have to maintain 2 codebases, one for your user interfaces and another for your C++ application logic.  


Platform Controls

Another strategy is to use the platform specific libraries to render the user interface controls. In this scenario, you will have to maintain 3 separate codebases. One for your iOS UI written in Objective-C, another for your Android UI written in Java, and a third codebase for your application logic written in C++.  Both Android and iOS fully support C++ but have different implementation approaches. On iOS, C++ is fully supported in the Objective-C CLANG compiler and you can even write mixed language files (.mm) however you will have to build bindings between the two languages to make Objective-C Cocoa SDK calls. On Android, C++ is supported in the Native Development Kit (NDK) and although much of the Android support can be found in the NDK, you will often have to make Java Native Interface (JNI) calls into Dalvik (the Android Java based framework) for some services and will certainly have to do this to talk back and forth with Dalvik based UIs. 

The advantage of platform rendered controls, when compared to using HTML 5, is greater performance for implemented functionality within your apps look-and-feel. 

The biggest disadvantage is to maintain 3 separate codebases as discussed above. That also means several different development environments as well, using Xcode and Interface Designer for iOS, Eclipse ADT for Android, and whichever editor/IDE you prefer for C++. On a side note both Xcode and ADT can support C++ as well although there level of support for C++ specific features including code completion, syntax highlighting, refactoring, and debugging can vary between one another.


C++ Framework Controls

There does exist a 3rd option for C++ developers and that is using a framework that implements an application stack that include user interface controls for both iOS and Android (recall ANSI/ISO C++ does not specify a user interface API), one example being Appmethod’s framework. In this scenario, the application logic and user interface controls are written with one language and can, in most cases, allow the development team to manage a single C++ source code base. I say most cases, because sometimes there is a need to build separate UIs for different form factors.

In this scenario, the C++ developer talks directly to his application code from UI events managed in C++, no need to write a binding layer. He can render controls via a C++ API that either calls the platform to render the control (and registers a callback for any data in the control, if needed) or the framework can owner draw the control using a hardware canvas managed by the GPU (i.e. OpenGL ES). 

There are many advantages to this approach, including using C++ for the whole application. Also, there is no need to manage a binding layer from the UI language code to the application logic code. The only downside with this approach is with regard to accessing platform specific APIs or services, not provided by the framework, may require some binding work. At least on iOS it is not an issue since you can mix/match with Objective-C++ but some Android services may require JNI calls. 


If you are a C++ developer, you have choices to continue to use your language of preference (or re-use existing source code) across the two most popular mobile operating systems.  These strategies work equally well across all other operating system platforms, desktop or mobile, since C++ is well supported on Windows and Windows Phone (include WinRT), MacOS X, BlackBerry, etc.  If you have not already invested in user interface development, special consideration should be given to application frameworks that can provide a common, cross-platform C++ API, including UI controls, to deliver not only the most productive development approach but also give you the full power of C++ across your complete application stack.

Tags: Appmethod

Gold User, Rank: 8, Points: 399
Brian Alexakis is a Product Marketing Manager at Embarcadero Technologies. He is focused on leveraging the connected world of technology to build new experiences for the Internet of Things.


Check out more tips and tricks in this development video: