我正在尝试在Android Studio和CMake中创建一个原生C++代码。我的C++代码使用预编译的静态库(.a文件)。我在我的C++代码中包含了它的头.h。我还在我的CMakeList.txt中链接了.h和.a文件的位置,如下所示:
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以包含有关库文件的信息?
发布于 2020-02-21 06:00:23
这是我的解决方案:首先,一开始使用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-你使用的任何实际的库,它的属性.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})然后,应该提到,为了确保预先构建的.a文件是兼容的,我们希望它采用什么样的abi格式。
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'
}
}https://stackoverflow.com/questions/60221472
复制相似问题