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
Table of contents Overview Vuex four major object...
Use the FRAME property to control the style type ...
The party that creates a new connection is equiva...
eureka: 1. Build a JDK image Start the eureka con...
Effect There are currently 2 projects (project1, ...
1. Introduction The main value that SELinux bring...
Problem description: After executing docker run -...
Table of contents First install wget View Help Ma...
MySQL (5.6 and below) parses json #json parsing f...
Sort Tool The Linux sort command is used to sort ...
Table of contents 1. Scenario description: 2. Cas...
This article shares with you the installation and...
I encountered such a problem during development A...
1. Command Introduction The cal (calendar) comman...