6

I am using Android Studio 2.2.2 with cmake and Android NDK. I have a problem linking .a library (Static lib).

Here is my 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) 

My linker reports an error

arm-linux-androideabi/bin\ld: error: cannot find -ltheoraplayer

error: undefined reference to 'TheoraVideoManager::TheoraVideoManager(int)'

which is a part of libtheoraplayer.a. Did anyone had similar problem? Any idea how to solve this?

I have the Static lib libtheoraplayer.a present at that location. I even have the Shared lib also, libtheoraplayer.so but I can`t link it either.

Any advice would be appreciated.

Cheers.

12
  • I have the Static lib libtheoraplayer.a present at that location. - Where exactly do you have this library? I see no link_directories calls in you code, so why do you expect a linker to find the library? Commented Nov 1, 2016 at 10:58
  • I am new to cmake so please forgive me if I ask stupid questions. I used the old system with Android.mk for Android NDK. I am not aware that I need link_directories? The lib is the Theora Player already built (theora.org) and I need to import it to my project. link_directioris should point to the source file of Theora? Commented Nov 1, 2016 at 11:02
  • 1
    You need to point CMake where it should search library for link. There are several ways to do that: Using absolute path to the library file in target_link_libraries, or using IMPORTed library target with absolute path, or using 'link_directories' call with directory where to search the library. In all cases the library file is libtheoraplayer.a, which you mention in the question. Commented Nov 1, 2016 at 11:16
  • Well I have TARGET_LINK_LIBRARIES(game-lib libtheoraplayer.a) at the end of the file, please check the file I added in the question. I will try to move this to another location and give a relative path to it. Commented Nov 1, 2016 at 11:28
  • Again, you use non-absolute filename for library. CMake (linker) tries to find this library .. and fails. This is what the error message talks about. CMake doesn't automatically search libraries in current source/build directory, you need to specify this explicitely using one of the ways I describe above. Commented Nov 1, 2016 at 11:36

1 Answer 1

8

To post the answer. As Tsyvarev said, the problem with non-absolute file name for library. When I used absoulte path it worked like a charm.

Thank you. Cheers.

Sign up to request clarification or add additional context in comments.

2 Comments

I managed to link .a libraries with relative path too. Here is an example: target_link_libraries(game ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/cricket/lib/libck.a)
You should put your comment in the answer because the comment is the answer. A bit frustrating having to edit this when upgrading Android studio. But it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.