【发布时间】:2020-12-14 01:24:21
【问题描述】:
我需要一个虚拟的 .so 文件才能在 android 中调用它的函数。虚拟 .so 文件可以是任何加法、减法等操作。 在线搜索时,我发现的只是“什么是 .so 文件?”,“如何在 android 中集成 .so 文件”。任何人都可以分享一个虚拟的 .so 文件来做加法吗?
【问题讨论】:
标签: android c++11 shared-libraries ndk-build .so
我需要一个虚拟的 .so 文件才能在 android 中调用它的函数。虚拟 .so 文件可以是任何加法、减法等操作。 在线搜索时,我发现的只是“什么是 .so 文件?”,“如何在 android 中集成 .so 文件”。任何人都可以分享一个虚拟的 .so 文件来做加法吗?
【问题讨论】:
标签: android c++11 shared-libraries ndk-build .so
这是我的第一个共享库,而且很容易做到。我已经申请了this toutorial。
这是我的代码:
lib.h
#pragma once
int sum(const int a, const int b);
lib.cpp
#include "lib.h"
int sum(const int a, const int b)
{
return a+b;
}
main.cpp
#include "lib.h"
#include <iostream>
int main()
{
std::cout << sum(10, 20) << std::endl;
return 0;
}
用这些命令我创建了一个:
# generating simple library
c++ -c -Wall -Werror -fPIC lib.cpp
# making it shared
c++ -shared -o libsum.so lib.o
现在您将拥有共享库,为了使该程序正常工作,我必须执行 2 个附加命令:
# create executable
g++ -L. main.cpp -l:libsum.so -o main
# if you try to run this command it will fail
./main
# so i've set ld path, but in the link above there are alternative solutions to that
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
# now it should work, in my case output is: 30
./main
【讨论】: