Android NDK error when linking shared C library(链接共享 C 库时出现 Android NDK 错误)
问题描述
I'm trying to link some C files to an NDK project that I'm working on, and set my CMakeLists.txt
file up like below.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall")
find_library( # Sets the name of the path variable.
log-lib
log)
add_library( # Specifies the name of the library.
main SHARED
main.c
communication_api.c
cybtldr_api.c
cybtldr_parse.c
cybtldr_command.c
)
target_link_libraries(main
communication_api
cybtldr_api
cybtldr_parse
cybtldr_command
${log-lib})
I'm getting the error on the step where it's Linking those libraries
[6/6] Linking C shared library /Users/rafa/Code/Labconco-FreezeDry-Android-Refactor/app/build/intermediates/cmake/debug/obj/x86/libmain.so
And error is pretty long
FAILED: : && /Users/rafa/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=i686-none-linux-android19 --gcc-toolchain=/Users/rafa/Library/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/rafa/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/rafa/Library/Android/sdk/ndk-bundle/sysroot/usr/include/i686-linux-android -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -std=c99 -Wall -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /Users/rafa/Library/Android/sdk/ndk-bundle/platforms/android-19/arch-x86 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/Users/rafa/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libmain.so -o /Users/rafa/Code/Labconco-FreezeDry-Android-Refactor/app/build/intermediates/cmake/debug/obj/x86/libmain.so CMakeFiles/main.dir/main.c.o CMakeFiles/main.dir/communication_api.c.o CMakeFiles/main.dir/cybtldr_api.c.o CMakeFiles/main.dir/cybtldr_parse.c.o CMakeFiles/main.dir/cybtldr_command.c.o -lcommunication_api -lcybtldr_api -lcybtldr_parse -lcybtldr_command -llog -latomic -lm && :
/Users/rafa/Library/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/darwin-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: error: cannot find -
lcommunication_api
/Users/rafa/Library/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/darwin-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: error: cannot find -lcybtldr_api
/Users/rafa/Library/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/darwin-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: error: cannot find -lcybtldr_parse
/Users/rafa/Library/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/darwin-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: error: cannot find -lcybtldr_command
The gist is this
error: cannot find -lcybtldr_command
error: cannot find -lcybtldr_api
error: cannot find -lcybtldr_parse
error: cannot find -lcybtldr_command
It looks like it's happening when it's trying to link all of the files in the add_library()
besides the main
which is in the same directory as the other files it can't link
What am I missing?
I think you misunderstand the cmake syntax. Below is enough for your case.
target_link_libraries(
main
${log-lib})
Below are source files, NOT library names.
communication_api
cybtldr_api
cybtldr_parse
cybtldr_command
So, you cmake statements are not correct.
If you want to make less confusing, try to make below changes.
find_library( # Sets the name of the path variable.
log-lib
log)
add_library( # Specifies the name of the library.
my-native-lib SHARED
main.c
communication_api.c
cybtldr_api.c
cybtldr_parse.c
cybtldr_command.c
)
target_link_libraries(my-native-lib
${log-lib})
But, remember to change your Java side as well, see below example:
// Used to load the 'my-native-lib' library on application startup.
static {
System.loadLibrary("my-native-lib");
}
I just enclose my JniExample project in case you need: https://github.com/russell-shizhen/JniExample
这篇关于链接共享 C 库时出现 Android NDK 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:链接共享 C 库时出现 Android NDK 错误
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01