How to create a shared library in C ++ for Android and iOS?

I use LibGDX to write applications for Android and iOS, and I want to be able to add C ++ code to my applications to optimize certain parts and to transfer some functions, etc.

I searched the Internet and tried to follow some tutorials, but did not find what I needed.

How can I write a very basic C ++ library that I can load into LibGDX? What tools do I need to use? Visual studio I am developing in Android Studio.

I think I need a .so file for Android and a .a file for iOS, is this correct?

+8
c ++ android ios android-ndk libgdx
source share
1 answer

On both platforms, you can directly enable the precompiled library, as well as the C ++ source code.

On Android, you'll want to learn the Android NDK . This allows you to include native code in C / C ++, which can switch to Java. The relationship between Java and C / C ++ is controlled using JNI . This is a rather tedious, inconvenient system for communication between C ++ and Java. You will want to study the customization of the makefile of the Android.mk file, which indicates how to include your library (or source code) in your assembly.

On iOS, it's a little more closely connected. You may have Objective-C ++ files that can run both C ++ code and Objective-C. If you use Swift, this is a bit different (the bridge between Objective-C ++ and Swift).

In some cases, when a platform (Android / iOS) provides functionality that is superior to what is possible or realistic with C ++, you may find yourself with a code architecture so that your C ++ can reach the platform if necessary. This means that you may have headers with separate implementation files per platform.

  • thing.h
  • thing_android.cpp
  • thing_ios.mm

The Android.mk Android application file will contain thing_android.cpp (but not thing_ios.mm ). This file can cross the JNI bridge to talk to Java as needed, whenever you need something from the Android SDK.

The iOS app will include thing_ios.mm (but not thing_android.cpp ). The .mm extension means Objective-C ++, so the file can directly access Cocoa's powerful libraries as needed.

Finally, on all platforms, you will either want to change your C ++ usage to the lowest common denominator platform. In other words, if iOS supports a specific C ++ function, but Android does not, then you cannot use this particular function.

+5
source share

All Articles