为啥 CMake 没有与我的自定义库(add_library/set_property)链接?
Posted
技术标签:
【中文标题】为啥 CMake 没有与我的自定义库(add_library/set_property)链接?【英文标题】:Why is CMake not linking with my custom library (add_library/set_property)?为什么 CMake 没有与我的自定义库(add_library/set_property)链接? 【发布时间】:2021-05-19 09:09:23 【问题描述】:我已经看到this question 的答案对我来说太宽泛了。显然这是一个仅与 Cmakelists.txt 相关的问题。
我正在尝试将动态 (.so) 本机库与支持 JNI 的现有 android 应用程序集成。
所以基本上,这个项目已经包含了一个共享本机库,该库将使用其他共享本机库。在我的例子中,它被称为 libcardios,它将被 native-lib.cpp 调用。
我想我这里有一个 CMakeLists.txt 问题,因为链接器命令根本不包含对我的共享库的引用:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("myhelloworldapplication")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
# the file list must be regenerated each time the repo is updated from bitbucket
# You can use a shell command like this one:
# find . -name '*.cpp' > source_code_list.txt
# then remove the portion of the path you do not want
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
)
# The folder list may have changed /!\
# Make sure to have it in sync with the repo
target_include_directories(native-lib PUBLIC
libcardios/includes
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
find_library( # Sets the name of the path variable.
android-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
android )
add_library(libcardios_lib SHARED IMPORTED)
set_property(TARGET libcardios_lib PROPERTY IMPORTED_LOCATION "libcardios/$CMAKE_ANDROID_ARCH_ABI/libcardios.so")
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
$log-lib
$android-lib
$libcardios_lib
)
这里是 build.gradle 文件的相关部分:
plugins
id 'com.android.application'
android
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig
applicationId "com.example.myhelloworldapplication"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild
cmake
cppFlags "-v -std=c++11 -fexceptions"
ndk
abiFilters "armeabi-v7a"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
externalNativeBuild
cmake
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
dependencies
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
最后是链接器命令的详细输出,它没有任何 libcardios 库的踪迹。
"/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" --sysroot=/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -z noexecstack -EL --warn-shared-textrel -z now -z relro -X --hash-style=both --enable-new-dtags --eh-frame-hdr -m armelf_linux_eabi -shared -o /Users/omatrot/Documents/sensoria_analyics/simpleandroidappwithlibcardios/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so /Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/16/crtbegin_so.o -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/9.0.8/lib/linux/arm -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/armv7-a/thumb -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a/thumb -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/16 -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/../lib -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/../../lib -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a/thumb -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib --exclude-libs libgcc.a --exclude-libs libgcc_real.a --exclude-libs libatomic.a --build-id --fatal-warnings --exclude-libs libunwind.a --no-undefined -soname libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o -llog -landroid -latomic -lm -Bstatic -lc++ -Bdynamic -lm -lgcc -ldl -lc -lgcc -ldl /Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/16/crtend_so.o
/Users/omatrot/Documents/sensoria_analyics/simpleandroidappwithlibcardios/app/src/main/cpp/native-lib.cpp:16: error: undefined reference to 'CardioS::init()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
问题可能出在哪里?这是 CMakeLists.txt 中指令的错误顺序吗?在我看来,$libcardios_lib 只是空的......
任何帮助表示赞赏。
【问题讨论】:
命令add_library(libcardios_lib SHARED IMPORTED)
创建一个名为libcardios_lib
的目标,而不是一个变量。与 target 的链接使用其名称执行:target_link_libraries(native-lib PUBLIC libcardios_lib)
。取消引用 ($..
) 只能应用于变量。取消引用目标名称没有意义。
你是对的。你能添加一个答案让我接受吗?提前致谢。
【参考方案1】:
与使用 add_library
创建的任何其他库一样,与 IMPORTED 库的链接是使用其名称执行的,无需取消引用它。
取消引用 ($...
) 应用于变量,但add_library
创建的是目标,而不是变量。 p>
add_library(libcardios_lib SHARED IMPORTED)
...
# Correct usage of the library:
target_link_libraries(native-lib PUBLIC libcardios_lib)
# Incorrect:
target_link_libraries(native-lib PUBLIC $libcardios_lib)
【讨论】:
以上是关于为啥 CMake 没有与我的自定义库(add_library/set_property)链接?的主要内容,如果未能解决你的问题,请参考以下文章