【问题标题】:Android NDK Cmake Linking .a (Static) LibsAndroid NDK Cmake 链接 .a(静态)库
【发布时间】:2017-03-14 11:47:49
【问题描述】:

我正在使用带有 cmake 和 Android NDK 的 Android Studio 2.2.2。我在链接 .a 库(静态库)时遇到问题。

这是我的 cmake:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)

# 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 it for you.    
# Gradle automatically packages shared libraries with your APK.  

add_library(lib_webp SHARED IMPORTED )
set_target_properties(lib_webp PROPERTIES IMPORTED_LOCATION
src/main/jni/${ANDROID_ABI}/libwebp.so)


add_library( # Sets the name of the library.
         game-lib

         # Sets the library as a shared library.
         SHARED

         # Provides a relative path to your source file(s).
         # Associated headers in the same location as their source
         # file are automatically included.
         src/main/cpp/main.cpp
         src/main/cpp/android_native_app_glue.c            
         )


target_include_directories(game-lib PRIVATE
 ../../../../libs/headers/android
)

include_directories($ENV{NDK_MODULE_PATH}/sources/android/native_app_glue/)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                   game-lib

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


                     # Specifies the name of the NDK library that
                     # you want CMake to locate.
                     log                        
                     android
                     OpenSLES
                     z
                     GLESv2
                     EGL
                     dl                         
                  )

add_definitions(-g -DANDROID -Wno-write-strings -fsigned-char -Wno-conversion-null)

TARGET_LINK_LIBRARIES(game-lib libtheoraplayer.a)

我的链接器报告错误

arm-linux-androideabi/bin\ld: 错误:找不到-ltheoraplayer

错误:未定义对“TheoraVideoManager::TheoraVideoManager(int)”的引用

这是 libtheoraplayer.a 的一部分。有没有人有类似的问题?知道如何解决这个问题吗?

我在那个位置有静态库 libtheoraplayer.a。我什至还有共享库 libtheoraplayer.so,但我也无法链接它。

任何建议将不胜感激。

干杯。

【问题讨论】:

  • I have the Static lib libtheoraplayer.a present at that location. - 究竟你在哪里有这个库?我在您的代码中没有看到 link_directories 调用,那么您为什么希望链接器找到该库?
  • 我是 cmake 新手,所以如果我提出愚蠢的问题,请原谅我。我将旧系统与 Android.mk 一起用于 Android NDK。我不知道我需要link_directories?该库是已经构建的 Theora Player (theora.org),我需要将其导入到我的项目中。 link_directioris 应该指向 Theora 的源文件?
  • 您需要将 CMake 指向它应该搜索链接库的位置。有几种方法可以做到这一点:使用 absolute pathtarget_link_libraries 中的库文件,或使用带有绝对路径的 IMPORTed library target ,或使用带有 directory 的 'link_directories' 调用来搜索库。在所有情况下,库文件都是libtheoraplayer.a,您在问题中提到。
  • 好吧,我在文件末尾有 TARGET_LINK_LIBRARIES(game-lib libtheoraplayer.a),请检查我在问题中添加的文件。我将尝试将其移动到另一个位置并给出它的相对路径。
  • 再次,您使用 非绝对文件名 作为库。 CMake(链接器)试图找到这个库..但失败了。这就是错误消息所说的内容。 CMake 不会自动搜索当前源/构建目录中的库,您需要使用我上面描述的一种方式明确地指定这个。

标签: android android-ndk linker cmake linker-errors


【解决方案1】:

发布答案。正如 Tsyvarev 所说,库的非绝对文件名的问题。当我使用绝对路径时,它就像一个魅力。

谢谢。 干杯。

【讨论】:

  • 我也设法将 .a 库与相对路径链接起来。这是一个示例:target_link_libraries(game ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/cricket/lib/libck.a)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
相关资源
最近更新 更多