问题
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