如何在c++中为Android和iOS创建一个共享库?

时间:2022-09-01 16:54:25

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.

我正在使用LibGDX为Android和iOS编写应用程序,我希望能够在我的应用程序中添加c++代码来优化某些部分并移植一些功能等。

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.

如何编写一个非常基本的c++库,我可以在LibGDX中加载它?我需要使用哪些工具?Visual Studio ?我在安卓工作室开发。

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

我想我需要一个Android的文件和一个iOS的文件,对吗?

1 个解决方案

#1


5  

On both platforms, it's possible to include a precompiled library as well as C++ source code directly.

在这两个平台上,都可以直接包含预编译库和c++源代码。

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.

在Android上,您需要考虑使用Android NDK。这允许您包含可以连接到Java的本机C/ c++代码。Java和C/ c++的连接是用JNI来管理的。这是一个相当乏味的系统,用于在c++和Java之间进行通信。你会想要建立一个Android。mkmakefile,它指定如何将您的库(或源代码)包含到您的构建中。

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).

在iOS系统中,链接更紧密。您可以有objective - c++文件,可以运行c++和Objective-C代码。如果您使用的是Swift,那么它有一点不同(在objective - c++和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.

在某些情况下,当平台(Android/iOS)提供的功能优于使用c++可能实现的或现实的功能时,您可能会发现自己构建了代码,以便您的c++可以根据需要访问平台。这意味着您可能拥有每个平台都有独立实现文件的头文件。

  • thing.h
  • thing.h
  • thing_android.cpp
  • thing_android.cpp
  • thing_ios.mm
  • 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.

android应用的android。mkfile将包含thing_android。cpp(但不是thing_ios.mm)。当您需要从Android SDK中获得一些东西时,这个文件可以通过JNI桥与Java对话。

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.

iOS应用程序将包括thing_ios。毫米(但不是thing_android.cpp)。扩展名.mm表示Objective-C+,因此该文件可以根据需要直接调用强大的Cocoa库。

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.

最后,在所有平台上,您都希望确保将c++的使用减少到最小的公共平台。换句话说,如果iOS支持c++的某个特性,而Android不支持,那么就不能使用该特性。

#1


5  

On both platforms, it's possible to include a precompiled library as well as C++ source code directly.

在这两个平台上,都可以直接包含预编译库和c++源代码。

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.

在Android上,您需要考虑使用Android NDK。这允许您包含可以连接到Java的本机C/ c++代码。Java和C/ c++的连接是用JNI来管理的。这是一个相当乏味的系统,用于在c++和Java之间进行通信。你会想要建立一个Android。mkmakefile,它指定如何将您的库(或源代码)包含到您的构建中。

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).

在iOS系统中,链接更紧密。您可以有objective - c++文件,可以运行c++和Objective-C代码。如果您使用的是Swift,那么它有一点不同(在objective - c++和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.

在某些情况下,当平台(Android/iOS)提供的功能优于使用c++可能实现的或现实的功能时,您可能会发现自己构建了代码,以便您的c++可以根据需要访问平台。这意味着您可能拥有每个平台都有独立实现文件的头文件。

  • thing.h
  • thing.h
  • thing_android.cpp
  • thing_android.cpp
  • thing_ios.mm
  • 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.

android应用的android。mkfile将包含thing_android。cpp(但不是thing_ios.mm)。当您需要从Android SDK中获得一些东西时,这个文件可以通过JNI桥与Java对话。

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.

iOS应用程序将包括thing_ios。毫米(但不是thing_android.cpp)。扩展名.mm表示Objective-C+,因此该文件可以根据需要直接调用强大的Cocoa库。

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.

最后,在所有平台上,您都希望确保将c++的使用减少到最小的公共平台。换句话说,如果iOS支持c++的某个特性,而Android不支持,那么就不能使用该特性。