c++动态编译 并调用

c++编译为动态库,并调用;实现封装到cpp里面,通过so使用

头文件

# test.h
#ifndef CIRCLE_H
#define CIRCLE_H

#include <iostream>
using namespace std;
class Demo
{
public:
    int do_mapping(std::string r, int m, bool e, int type);
};

#endif

 实现

#test.cpp
#include "Demo.h"
using namespace std;

int Demo::do_mapping(std::string r, int m, bool e, int type){
	std::cout << "r:" << r<< std::endl;
	std::cout << "m:" << m<< std::endl;
	std::cout << "e:" << e<< std::endl;
	std::cout << "type" << type << std::endl;
	return 1;
}

 

测试 main.cpp:

#include <iostream>
#include "Demo.cpp"
int main( ){
	Demo* demo = new Demo();
    demo->do_mapping("a",100,true,0);
    cout<<"end"<<endl;
    return 0;
}

调用

g++ main.cpp

  

编so 动态链接库(名字:开头 lib, so结尾)

g++ Demo.cpp -fPIC -shared -o libdemo.so

调用动态连接库 testso.cpp

#include <iostream>
#include "Demo.h"
int main( ){
	Demo* demo = new Demo();
    demo->do_mapping("a",100,true,0);
    cout<<"end"<<endl;
    return 0;
}

  调用so(头文件 *.h 需要和so放到一个目录中)

g++ testso.cpp  -L. -ldemo -o o.txt
more o.txt

  

 

参考:

https://www.cnblogs.com/wnnily/articles/4565237.html

https://www.linuxidc.com/Linux/2012-09/70502.htm

转载于:https://www.cnblogs.com/dasheng-maritime/p/10973372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值