使用CMake将静态库(.a文件)添加到Android项目中,获取"CMake错误:CMake无法确定目标的链接器语言". [英] Add a static library (.a file) to an Android project with CMake, get "CMake Error: CMake can not determine linker language for target"
问题描述
我从另一个Android项目生成了静态库,因此非常确定它们是可用的.
I generate the static library from another Android project, so pretty sure they're useable.
我得到了四个基于CPU架构的.a文件,其中一个.h文件也已经过测试.
I got four .a files based on CPU architectures, one .h file which also has been tested.
现在在新项目中,另一个.c文件要调用静态库,我无法将两个项目合并,必须以.a格式调用静态库.
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.
我收到"CMake错误:CMake无法确定目标的链接器语言",这是我的CMakeLists.txt:
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是预构建的库.native-lib要调用mylib.
mylib is the prebuilt library. native-lib want to call mylib.
也欢迎提供有关如何从scrath向项目添加.a文件的链接.
A link about how to add .a file to a project from scrath is also welcome.
推荐答案
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)
如您所见,我将这些 .a
文件放入
As you can see, i put those .a
files in
projectNmae \ app \ src \ main \ jniLibs \ $ {ANDROID_ABI} \
,
如果更改位置,请记住在 CMakeList.txt
中声明它.
if you change the location, remember to declare it in the CMakeList.txt
.
我将 my_static_lib.h
放在 src \ main \ include
中,并在其他 .c/cpp
文件中使用它,例如:>
I put my_static_lib.h
in src\main\include
, and use it in other .c/cpp
file like:
#include "../include/features.h"
我的 BIGGEST 错误是错过的:
${CMAKE_SOURCE_DIR}
CMake无法在 src/main/app/native-lib.c
, SHARED 库很好的位置找到 STATIC 库 STATIC 库,这很奇怪.
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.
这篇关于使用CMake将静态库(.a文件)添加到Android项目中,获取"CMake错误:CMake无法确定目标的链接器语言".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!