【发布时间】:2020-09-03 08:40:27
【问题描述】:
我正在尝试使用 JNI 构建一个 android 应用程序。以下是我的C++代码结构:
main
-cpp
-native-lib.cpp
-dependency.h
-dependency.cpp
在我的native-lib.cpp 中包括dependency.h。在我的 Cmake 中,我正在为 dependency 和 native-lib 创建一个共享库 (.so),以便我可以将它们加载到我的 Java 代码部分中。
在构建代码时,我无法构建 native-lib,因为它依赖于 dependency。为了快速修复,我首先构建dependency,然后将其链接到native-lib,然后将两者加载到我的java端。但我知道这是错误的做事方式,因此我想知道在 JNI 中添加多个依赖项的正确方法。
以下是我的 Cmake:
cmake_minimum_required(VERSION 3.4.1)
include_directories(Include)
add_library( # Sets the name of the library.
dependency
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
dependency.cpp )
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Once the so is being built I'm adding it myself and linking it.
# I don't want to do this but link it automatically
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libdependency.so)
这是Java代码:
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("dependency");
System.loadLibrary("native-lib");
}
// Some other functions
}
PS:不将target_link_libraries 添加到链接libdependency.so 认为Java LoadLibrary 将实时链接它不会构建,因为native-lib 将具有未定义的引用
【问题讨论】:
-
为什么不简单地
target_link_libraries(native-lib dependency)?然后 CMake 将在Makefile中生成正确的构建依赖项。 -
我确实尝试过,但它不起作用。在将这个问题放在 ps 部分之前,我也尝试过同样的方法。每次我这样做时它都有效,但这次我知道发生了什么,因此问题
标签: java android c++ cmake java-native-interface