I am using LibGDX to write apps for both Android and iOS and I want to be able to add C++ code to my apps to optimize certain parts and to port some functions etc.
I have been searching the internet and tried to follow some tutorials, but did not find what I need.
How can I write a very basic C++ library which I can load in LibGDX? Which tools do I need to use? Visual Studio? I develop in Android Studio.
I think that I need an .so file for Android and an .a file for iOS, is that correct?
Similar to the traditional Linux model, shared libraries in Android are relocatable ELF files that map to the address space of the process when loaded. To save memory and avoid code duplication, all shared objects shipped with Android are dynamically linked against the Bionic libc library [23].
You can most definitely use C++ on iOS and Android.
On both platforms, it's possible to include a precompiled library as well as C++ source code directly.
On Android, you'll want to look into using the Android NDK
. This allows you to include native C/C++ code that can bridge over to Java. The connection between Java and C/C++ is managed with the JNI
. It's a fairly tedious, awkward system for communicating between C++ and Java. You'll want to look into setting up an Android.mk
makefile that specifies how to include your library (or source code) into your build.
On iOS, it's a little more tightly linked. You can have Objective-C++ files that can run both C++ and Objective-C code. If you're using Swift, it's a little different (bridging between Objective-C++ and Swift).
In some cases, when the platform (Android/iOS) provides functionality that is superior to what is possible or realistic with C++, you might find yourself architecting the code such that your C++ can reach out to the platform as needed. This means that you might have headers with separate implementation files per platform.
thing.h
thing_android.cpp
thing_ios.mm
The android app's Android.mk
file will include thing_android.cpp
(but not thing_ios.mm
). This file could cross the JNI bridge to talk to Java as needed, whenever you need something from Android SDK.
The iOS app will include thing_ios.mm
(but not thing_android.cpp
). The .mm
extension signifies Objective-C++, so that file could directly call powerful Cocoa libraries as needed.
Finally, on all platforms, you'll want to be sure to either scale back your usage of C++ to the lowest common denominator platform. In other words, if iOS supports a particular feature of C++, and Android doesn't, then you cannot use that particular feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With