从CPP文件访问静态库libadd.a。
CPP文件是使用CMakeList.txt编译的,该文件会编译并由应用程序生成libnative-lib.so文件。
更新了CMakeList.txt
**target_link_libraries(
native-lib
${log-lib}
${CMAKE_SOURCE_DIR}/libadd.a)**
在native-lib.cpp文件中访问函数add()
#include <jni.h>
#include <string>
#include "../../../staticLib/files/add.hpp"
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_oncall_myapplication_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello SUCESSS its 12";
std::string hello1 = "Hello FAILED it is not 12";
addition::my::work::abc Obj;
Obj.add(5,7);
if(Obj.add(5,7) == 12)
{
return env->NewStringUTF(hello.c_str());
}
else
{
return env->NewStringUTF(hello1.c_str());
}
}
尝试构建代码时出错。
构建命令失败。 使用参数{--build /home/oncall/Bha/MyApplication3/app/.externalNativeBuild/cmake/debug/x86_64-执行进程/home/oncall/Desktop/Android/Sdk/cmake/3.6.4111459/bin/cmake时出错-target native-lib} [1/1]链接CXX共享库../../../../build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so 失败::&& / home / oncall / Desktop / Android / Sdk / ndk-bundle / toolchains / llvm / prebuilt / linux-x86_64 / bin / clang ++ --target = x86_64-none-linux-android21 --gcc-toolchain = /主页/ oncall /桌面/ Android / Sdk / ndk-捆绑/工具链/ llvm / prebuilt / linux-x86_64 --sysroot = / home / oncall / Desktop / Android / Sdk / ndk-bundle / toolchains / llvm / prebuilt / linux- x86_64 / sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong-无规范前缀-fno-addrsig -Wa,-noexecstack -Wformat -Werror = format-安全-stdlib = libc ++ -O0 -fno-limit-debug-info -Wl,-exclude-libs,libgcc.a -Wl,-exclude-libs,libatomic.a -static-libstdc ++ -Wl,-build- id -Wl,-警告共享文本-Wl,-致命警告-Wl,-无定义-Qunused参数-Wl,-z,noexecstack -Wl,-z,relro -Wl,-z ,现在-共享-Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so CMakeFiles / native- lib.dir / src / main / cpp / native-lib.cpp.o / home / oncall / Desktop / Android / Sdk / ndk-bundl e / toolchains / llvm / prebuilt / linux-x86_64 / sysroot / usr / lib / x86_64-linux-android / 21 / liblog.so ../../../../../libadd.a -latomic -lm &&: /home/oncall/Desktop/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x / .. / .... / x86_64-linux-android / bin / ld:错误:../../../../libadd.a(add.o):需要针对'_ZN8addition2my4work3abc5countE'的动态R_X86_64_PC32重定位,它可能在运行时溢出;用-fPIC重新编译 /home/oncall/Desktop/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x / .. / .... / x86_64-linux-android / bin / ld:警告:共享库文本段不可共享 ../../../../ libadd.a(add.o):add.cpp:function adding :: my :: work :: abc :: getCount():error:undefined reference to 'addition :: my :: work :: abc :: count' ../../../../libadd.a(add.o):add.cpp:function adding :: my :: work :: abc ::〜abc():错误:未定义对'addition的引用:: my :: work :: abc :: count' ../../../../libadd.a(add.o):add.cpp:function adding :: my :: work :: abc ::〜abc():错误:未定义对'addition的引用:: my :: work :: abc :: count' clang ++:错误:链接器命令失败,退出代码为1(使用-v查看调用) 忍者:构建停止:子命令失败。
libadd.a的add.cpp
#include "add.hpp"
namespace addition{
namespace my{
namespace work{
int abc::add(int x, int y) {
return (x+y);
}
int abc::getCount(){
return count;
}
abc::~abc(){
count--;
}
};
};
};