Ubuntu 16.04环境下QT生成so(动态链接库),
并新建QT项目调用so文件
1.首先是新建一个工程,选择Library ,然后选择Library C++库,打开后是这样的:
2.选择 “choose”,命名为“plugintest”(后面的plugintest即为同样流程建立的文件,plugintest0为了演示重新新建的项目)
3.点“next”之后,得到下图
4.继续“next”,得到下图
5.点“finish”,得到一个“plugintest”的工程,然后点菜单栏“build”的“rebuild project“plugintest””,然后点左下角的绿色三角按钮编译运行。
6.在plugintest.h修改为如下代码(红色为添加的代码):
#ifndef PLUGINTEST_H
#define PLUGINTEST_H
#include "plugintest_global.h"
class PLUGINTESTSHARED_EXPORT plugintest
{
public:
plugintest();
void helloworld();//新加入
};
#endif // PLUGINTEST_H
7.在plugintest.cpp修改为如下代码(红色为添加的代码):
#include "plugintest.h"
#include <iostream>
using namespace std;
plugintest::plugintest()
{
}
void plugintest::helloworld()//新加入
{//新加入
cout<<"hello world"<<endl;//新加入
}//新加入
8.再一次编译运行plugintest,项目的debug文件夹生成下面这些文件:(此时有弹出如下为成功)
9.新建C++项目,步骤:new project->non-qt project->plain c++ application->choose,然后一直next,直至新建到项目。
10.鼠标右击项目insert_to文件夹,选择“add library”,得到下图,选“next”(把生成的libplugintest.so文件和plugintest.h文件放在insert_to文件夹里):
10.得到类似与下图library file选刚才的So文件,include path选insert_to文件夹
11.一直next得到下图pro文件的截图
12.修改main.cpp,红色代码为加入新的内容,至此,调用完毕!
#include <iostream>
#include "plugintest.h"//包含plugintest.h头文件//新加入
using namespace std;//新加入
int main()
{
//cout << "Hello World!" << endl;
plugintest test;//PluginTest是plugintest.h中定义的类名//新加入
test.helloworld();//调用helloworld//新加入
return 0;
}