Android Studio 中带有静态库的原生C++代码
Native C++ Code in Android Studio with a Static Library
我正在尝试在Android Studio和CMake中制作本机C++代码。我的C++代码使用预编译的静态库(.a 文件(。 我在C++代码中包含其标头 .h。我还在我的CMakeList中链接了.h和.a文件的位置.txt如下所示:
include_directories(".h file location")
然后:
add_library(lib_fastcv STATIC IMPORTED)
set_target_properties(lib_fastcv PROPERTIES IMPORTED_LOCATION
".a file location")
最后:
target_link_libraries (...lib_fastcv....)
但是,一旦我使用 .a 静态库中的任何函数,它就会抱怨它无法识别该函数,这意味着静态库没有正确链接到我的C++代码。
有谁知道我还需要做什么? 我是否也应该编辑我的 build.gradle 以包含有关库文件的信息?
这是我的解决方案: 所以,首先,CMake 一开始使用起来可能很奇怪。
1- native-lib1 是 CMake 的输出产品。后来的java只会看到这个的.so
。add_library( # This is out .so product (libnative-lib1.so)
native-lib1
# Sets the library as a shared library.
SHARED
# These are the input .cpp source files
native-lib.cpp
SRC2.cpp
Any other cpp/c source file you want to compile
)
2-您在souse文件中包含的任何库,其.h都需要在此处解决:
target_include_directories(# This is out .so product (libnative-lib1.so)
native-lib1 PRIVATE
${CMAKE_SOURCE_DIR}/include)
3-您使用的任何实际库,其atual.a或.cpp都应以这种方式添加到CMake:
target_link_libraries(
# This is out .so product (libnative-lib1.so)
native-lib1
#These are any extra library files that I need for building my source .cpp files to final .so product
${CMAKE_SOURCE_DIR}/lib/libfastcv.a
# Links the target library to the log library
# included in the NDK.
${log-lib})
然后 build.gradle 应该提到我们希望它采用哪些 abi 格式,以确保您预先构建的 .a 文件兼容。
flavorDimensions "version"
productFlavors {
create("arm7") {
ndk.abiFilters("armeabi-v7a")
}
最后,请注意,以下命令无法过滤 abis 和上面的命令工作,即使它们在逻辑和形式上看起来与我相似:
cmake {
cppFlags "-std=c++11"
abiFilters 'armeabi-v7a'
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
// This is not working to eliminate
abiFilters 'armeabi-v7a'
}
}
相关文章:
- Android Studio 中带有静态库的原生C++代码
- 如何在 Android Studio 4 中编译 C/C++ 原生代码
- Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让原生C / C++
- 在Android原生C++中使用Facemark opencv contrib
- CMake:Android Studio 原生活动项目无法在项目根目录之外找到第三方库
- Android NDK - C++ 异常会导致第三方原生库崩溃
- 如何在 gradle 原生项目中包含二进制库?
- 使用 Flutter 从 Android 上的原生编译共享库中调用函数
- exteranl原生构建问题,构建命令失败,android工作室
- build命令失败,外部原生问题android工作室
- 如何在electrolon项目中集成qt原生窗口
- Lua :在原生lua中编译,但在使用LuaJIT和sol2时C++出错
- 如何将.cpp C++原生库导入 Android Studio
- 原生 WebRTC 丢帧
- 如何使用 JNI 创建原生类?
- 如何为原生UI工具包扩展Ranorex?
- 如何从反应原生代码调用 C 函数?
- 安卓设备上的原生C++ WiFi P2P 网络是否可行?
- Android Studio 调试原生代码 lldb 服务器工作无穷大
- Android 服务和原生线程