Android Studio CMakeLists错误: undefined reference to

标签 android c android-studio cmake java-native-interface

我有这个项目

enter image description here

我正在尝试使用 CMakeLists 构建 C 的源代码:

# 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.4.1)

# 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.

add_library( # Sets the name of the library.
             rtmp-jni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${CMAKE_SOURCE_DIR}/src/main/cpp/rtmp-jni.c)

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../jnisource"
                                        "${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/includes"
                                        "${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/includes/openssl"
                                        "${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/librtmp")

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

# 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.
                      rtmp-jni

                      # Links the target library to the log library
                      # included in the NDK.
                      ${log-lib} )

当我构建 APK 时,出现此错误:

Build command failed.
Error while executing process /Users/kevinabrioux/android-sdks/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target rtmp-jni}
[1/1] Linking C shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/librtmp-jni.so
FAILED: : && /Users/kevinabrioux/android-sdks/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang  --target=armv7-none-linux-androideabi --gcc-toolchain=/Users/kevinabrioux/android-sdks/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/kevinabrioux/android-sdks/ndk-bundle/sysroot -fPIC -isystem /Users/kevinabrioux/android-sdks/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security  -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a --sysroot /Users/kevinabrioux/android-sdks/ndk-bundle/platforms/android-21/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,librtmp-jni.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/librtmp-jni.so CMakeFiles/rtmp-jni.dir/src/main/cpp/rtmp-jni.c.o  -llog -lm && :
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:14: error: undefined reference to 'RTMP_Alloc'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:20: error: undefined reference to 'RTMP_Init'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:21: error: undefined reference to 'RTMP_SetupURL'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:26: error: undefined reference to 'RTMP_Connect'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:26: error: undefined reference to 'RTMP_ConnectStream'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:35: error: undefined reference to 'RTMP_Close'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:36: error: undefined reference to 'RTMP_Free'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:54: error: undefined reference to 'RTMP_Read'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:75: error: undefined reference to 'RTMP_Pause'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:84: error: undefined reference to 'RTMP_IsConnected'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:93: error: undefined reference to 'RTMP_IsTimedout'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:99: error: undefined reference to 'RTMP_Close'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:100: error: undefined reference to 'RTMP_Free'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

我该怎么做才能让我的项目找到这些对象的引用?

最佳答案

您的target_link_libraries必须包含librtmp。您不需要将其构建为项目的一部分。使用此库附带的 make 文件制作一次,并将其引用为 IMPORTED 就完全可以了,例如

add_library(shared-lib SHARED IMPORTED)
set_target_properties(shared-lib PROPERTIES IMPORTED_LOCATION
                  ${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/librtmp/lib/${ANDROID_ABI}/librtmp.so)

关于Android Studio CMakeLists错误: undefined reference to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52830744/

相关文章:

c++ - 链接问题与 g++ 有关,但与 cc 无关

java - Gradle 同步失败 : Unsupported method: SyncIssue. getMultiLineMessage()。 (安卓工作室)

android - 如何在 Studio 项目中禁用 Jacoco 测试覆盖率

java - Android Studio 有错误的 mail.jar 和 additionnal.jar 和 activation.jar

c - 如何循环打开和关闭文件

java - 从 calendar.getInstance 获取错误的日期

C: scanf 和字符串

android - Android Studio 升级后出现 NoSuchElementException

Android如何获取设备显示颜色

java - 无法在 Eclipse 中找到 SDK 文件夹