Add a static library (.a file) to an Android project with CMake, get “CMake Error: CMake can not determine linker language for target”

那年仲夏 提交于 2021-01-28 03:50:25

问题


I generate the static library from another Android project, so pretty sure they're useable.

I got four .a files based on CPU architectures, one .h file which also has been tested.

Now in new project, another .c file want to call the static library, i can't combine the two projects, the static libraries must be called in .a format.

I got "CMake Error: CMake can not determine linker language for target", this is my CMakeLists.txt:

add_library(
    mylib
    STATIC
    src/main/jniLibs/arm64-v8a/libmylib.a
    src/main/jniLibs/armeabi-v7a/libmylib.a
    src/main/jniLibs/x86/libmylib.a
    src/main/jniLibs/x86_64/libmylib.a
)

target_link_libraries(
    native-lib
    mylib
)

mylib is the prebuilt library. native-lib want to call mylib.

A link about how to add .a file to a project from scrath is also welcome.


回答1:


add_library(
        my_static_lib
        STATIC
        IMPORTED
)
set_target_properties(features PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libmy_static_lib.a)

As you can see, i put those .a files in

projectNmae\app\src\main\jniLibs\${ANDROID_ABI}\,

if you change the location, remember to declare it in the CMakeList.txt.

I put my_static_lib.h in src\main\include, and use it in other .c/cpp file like:

#include "../include/features.h"

My BIGGEST mistake is the missed:

${CMAKE_SOURCE_DIR}

CMake cannot find STATIC library in locations like src/main/app/native-lib.c, SHARED library is OK, not STATIC library, which is very strange.

And IMPORTED is a must, i tried replace it with the whole location path, won't work.

Thank you all the comments and answers, i hope this answer can help newbies like me.




回答2:


You should link your existing static libs to your current shared library, i.e. .so. For example, you have src/c/dummy.c. Then your CMakeLists.txt should be something like below:

add_library(my-shared-lib SHARED src/c/dummy.c)

target_link_libraries(my-shared-lib -Wl, --whole-archive src/main/jniLibs/${ANDROID_ABI}/libmylib.a -Wl,--no-whole-archive)

Explanations

  • ${ANDROID_ABI} is the CMake variable to identify the current ABI being built, i.e. arm64-v8a, arm64-v8a, x86, x86_64.


来源:https://stackoverflow.com/questions/52947801/add-a-static-library-a-file-to-an-android-project-with-cmake-get-cmake-erro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!