c++編譯爲動態庫,並調用;實現封裝到cpp裏面,經過so使用html
頭文件linux
# 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
實現ios
#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:c++
#include <iostream> #include "Demo.cpp" int main( ){ Demo* demo = new Demo(); demo->do_mapping("a",100,true,0); cout<<"end"<<endl; return 0; }
調用app
g++ main.cpp
編so 動態連接庫(名字:開頭 lib, so結尾)測試
g++ Demo.cpp -fPIC -shared -o libdemo.so
調用動態鏈接庫 testso.cppspa
#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放到一個目錄中)htm
g++ testso.cpp -L. -ldemo -o o.txt more o.txt
參考:blog