Detailed explanation of dynamic link library calling C/C++ method in Python in Ubuntu

Detailed explanation of dynamic link library calling C/C++ method in Python in Ubuntu

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:
  • Implementing a simple verification code with Python
  • Python uses Pillow (PIL) library to implement the whole process of verification code picture
  • Python image verification code recognition latest module muggle_ocr sample code
  • Python implements verification code recognition
  • How to create a dynamic link library dll using python
  • Detailed explanation of the basic process of Python calling dynamic link library
  • Python tutorial on calling dynamic link libraries in Windows and Linux
  • Python calls the Easy Language dynamic link library to implement the verification code function

<<:  How InnoDB implements serialization isolation level

>>:  Steps to encapsulate the carousel component in vue3.0

Recommend

Vue shuttle box realizes up and down movement

This article example shares the specific code for...

MySQL and MySQL Workbench Installation Tutorial under Ubuntu

Ubuntu install jdk: [link] Install Eclipse on Ubu...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

Linux yum package management method

Introduction yum (Yellow dog Updater, Modified) i...

How to create components in React

Table of contents Preface Component Introduction ...

MySql inserts data successfully but reports [Err] 1055 error solution

1. Question: I have been doing insert operations ...

Mysql5.7.14 Linux version password forgotten perfect solution

In the /etc/my.conf file, add the following line ...

WeChat applet uses canvas to draw clocks

This article shares the specific code of using ca...

Steps to deploy ingress-nginx on k8s

Table of contents Preface 1. Deployment and Confi...

CSS achieves the effect of two elements blending (sticky effect)

I remember that a few years ago, there was an int...

Solution to high CPU usage of Tomcat process

Table of contents Case Context switching overhead...

Useful codes for web page creation

<br />How can I remove the scroll bar on the...

Optimizing JavaScript and CSS to improve website performance

<br /> In the first and second parts, we int...