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
參考:
