链接共享C库时的Android NDK错误

 2023-01-19    371  

问题描述

我正在尝试将一些C文件链接到我正在处理的NDK项目,并设置我的CMakeLists.txt文件如下.

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})

我在链接这些库的步骤上遇到错误

链接共享C库时的Android NDK错误

[6/6] Linking C shared library /Users/rafa/Code/Labconco-FreezeDry-Android-Refactor/app/build/intermediates/cmake/debug/obj/x86/libmain.so

和错误很长

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

要点是这个

error: cannot find -lcybtldr_command
error: cannot find -lcybtldr_api
error: cannot find -lcybtldr_parse
error: cannot find -lcybtldr_command

似乎在尝试链接add_library()中的所有文件时,它正在发生,除main之外,该文件与其他文件在同一目录中,它无法链接

我缺少什么?

推荐答案

我认为您误解了CMAKE语法.以下足以让您的情况.

target_link_libraries(
    main
    ${log-lib})

以下是源文件,不是库名称.

communication_api
cybtldr_api
cybtldr_parse
cybtldr_command

因此,您的cmake语句不正确.

如果您想减少混乱,请尝试进行以下更改.

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})

但是,请记住要更改您的Java一侧,请参见下文示例:

// Used to load the 'my-native-lib' library on application startup.
static {
    System.loadLibrary("my-native-lib");
}

我只是附上了我的jniexample项目,以防您需要: Shizhen/jniexample

以上所述是小编给大家介绍的链接共享C库时的Android NDK错误,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/25870.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。