Install boost There are many ways to call C/C++ from Python. This article uses boost.python. Considering that there will be a lot of development work on boost in the later stage, boost is installed together. The Boost library is divided into two parts for use. One is to directly use the corresponding header file, and the other is to compile and install the corresponding library before it can be used. For specific installation methods, please refer to: https://www.jb51.net/article/150380.htm Here we use: sudo apt-get install libboost-all-dev Server Send after serialization main.cpp: #include <iostream> #include "libUO.h" int main() { UO_C_Socket t; // t.StartSocketServer("",4121); boost::thread t1(boost::bind(&UO_C_Socket::StartSocketServer,&t,"",4121)); sleep(2); // boost::thread t2(boost::bind(&UO_C_Socket::StartSocketClient,&t,"127.0.0.1",4121)); // t2.join(); t1.join(); return 0; } Client The client implements basic functions in UO_BaseFun.h, encapsulates them and exports them through boost_python. Note that the name in BOOST_PYTHON_MODULE must match the so file generated by the final make. Same name, otherwise there will be an error, the error name is forgotten UO_libdll_py_wrap.cpp: #include <boost/python.hpp> #include <boost/python/module.hpp> #include <boost/python/def.hpp> #include "UO_BaseFun.h" BOOST_PYTHON_MODULE(UO_BaseFun) //python module { // boost::python::class_<UO_C_Socket,boost::noncopyable>("UO_C_Socket") boost::python::class_<UO_C_Socket>("UO_C_Socket") .def("StartSocketClient",&UO_C_Socket::StartSocketClient) // .def("getname",&student::getname) // .def("setage",&student::setage) // .def("getage",&student::getage) // .add_property("name",&student::getname,&student::setname) // .add_property("age",&student::getage,&student::setage) ; } Pay special attention to the difference between compilation and connection in Makefile. Undefined symbol errors that occur require the addition of dynamic link libraries such as -lboost_filesystem. An error occurs: pyconfig.h cannot be found and needs to be included -I/usr/include/python2.7. After make is completed, UO_BaseFun.so file is generated Makefile: UO_BaseFun.so:UO_libdll_py_wrap.o g++ UO_libdll_py_wrap.o -o UO_BaseFun.so -shared -fPIC -L/usr/lib/x86_64-linux-gnu\ -lboost_filesystem -lboost_thread -lboost_serialization -lboost_python -lboost_system UO_STR.o: g++ -c UO_STR.h -o UO_STR.o -I/usr/include/boost \ # -lboost_serialization UO_BaseFun.o:UO_STR.o g++ -c UO_BaseFun.h -o UO_BaseFun.o -I/usr/include/boost \ # -lboost_system -lboost_filesystem -lboost_thread -lboost_serialization UO_libdll_py_wrap.o:UO_BaseFun.o g++ -c UO_libdll_py_wrap.cpp -o UO_libdll_py_wrap.o -fPIC -I/usr/include/python2.7 # -lboost_serialization clean: rm -rf UO_STR.o O_libdll_py_wrap.o UO_BaseFun.o rm -rf UO_BaseFun.so verify UO_StoreSystem_py.py: import UO_BaseFun test = UO_BaseFun.UO_C_Socket() test.StartSocketClient("127.0.0.1",4121) Summarize: The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM. You may also be interested in:
|
<<: How InnoDB implements serialization isolation level
>>: Steps to encapsulate the carousel component in vue3.0
This article example shares the specific code for...
Ubuntu install jdk: [link] Install Eclipse on Ubu...
This article shares the specific code of JavaScri...
Introduction yum (Yellow dog Updater, Modified) i...
In real life, a lock is a tool we use when we wan...
Table of contents Preface Component Introduction ...
1. Question: I have been doing insert operations ...
In the /etc/my.conf file, add the following line ...
This article shares the specific code of using ca...
Table of contents Preface 1. Deployment and Confi...
Table of contents Preface Six features of JSON.st...
I remember that a few years ago, there was an int...
Table of contents Case Context switching overhead...
<br />How can I remove the scroll bar on the...
<br /> In the first and second parts, we int...