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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM