Detail of dynamic link library for python to call C and C++ method in ubuntu

  • 2020-12-13 19:16:21
  • OfStack

Install boost

There are many ways that python can call C/C++, and this article uses boost.python. Considering that there was a lot of development work on boost in the later period, boost1 was installed and the Boost library was divided into two parts for use: 1 is to directly use the corresponding header file, and 2 is to compile and install the corresponding library.

Specific installation method can be reference: https: / / www ofstack. com article / 150380. htm

Use here:


sudo apt-get install libboost-all-dev 

The service side

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;
}

The client

The client implements the basic functions in ES39en_BaseFun.h, encapsulates it and exports it through boost_python. Note in particular that the name in BOOST_PYTHON_MODULE and the last so file from make are required

Same name, otherwise there will be an error, the wrong name 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 The 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)
 ;
}

Special attention is paid to the difference between compilation and connection in makefile, where undefined symbol errors need to be added to the dynamic link library -lboost_filesystem, etc. pyconfig. h error cannot be found

- I/usr/include/python2. 7. After the completion of make, the ES75en_ES76en.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

validation

UO_StoreSystem_py. py:


 import UO_BaseFun
test = UO_BaseFun.UO_C_Socket()
test.StartSocketClient("127.0.0.1",4121)

Conclusion:


Related articles: