Ubuntu--(5)Linux下C++编译生成自定义静态库/动态库

来源:互联网 发布:红辣椒电影数据分析 编辑:程序博客网 时间:2024/04/19 03:19

编译生成静态库:

1.编写CPP文件test.h

#include <iostream>using namespace std;class ADD_SUB{public:    int add(int a, int b){        cout << "a + b = " << a + b << endl;        return a + b;    }    int sub(int a, int b){        cout << "a - b = " << a - b << endl;        return a - b;    }};

2.编译

g++ -o test.out -c test.h

3.生成

ar -cr libtest.a test.out

4.调用libtest.a的main函数如下:

#include <iostream>#include "test.h"using namespace std;void main(){    ADD_SUB as;    as.add(1,3);    as.sub(1,3);}

5.编译

main.cpp: g++ main.cpp

6.运行:

./a.out

C++生成动态库

1.编写如生成静态库一样的test.h文件。

2.编译

g++ -shared -fPIC -o libtest.so  test.h

3.调用libtest.so的main函数与静态库一样

4.编译

g++ -o main.out main.cpp

5.运行测试

./main.out
0 0
原创粉丝点击