问题描述
我需要一个虚拟.so文件才能在android中调用它的功能。虚拟.so文件可以是进行加,减等操作的任何内容。 在网上搜索时,我发现的是“什么是.so文件?”,“如何在Android中集成.so文件”。任何人都可以共享一个虚拟的.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;
}
并使用theese命令创建了一个:
# 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