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

Understanding Vuex in one article

Table of contents Overview Vuex four major object...

HTML table tag tutorial (12): border style attribute FRAME

Use the FRAME property to control the style type ...

How to remotely connect to MySQL database with Navicat Premium

The party that creates a new connection is equiva...

How to configure eureka in docker

eureka: 1. Build a JDK image Start the eureka con...

...

Detailed explanation of SELINUX working principle

1. Introduction The main value that SELinux bring...

A detailed introduction to wget command in Linux

Table of contents First install wget View Help Ma...

Detailed example of MySQL (5.6 and below) parsing JSON

MySQL (5.6 and below) parses json #json parsing f...

Detailed explanation of Linux commands sort, uniq, tr tools

Sort Tool The Linux sort command is used to sort ...

How to recover data after accidentally deleting ibdata files in mysql5.7.33

Table of contents 1. Scenario description: 2. Cas...

Installation and configuration of mysql 8.0.15 under Centos7

This article shares with you the installation and...

Mysql transaction concurrency problem solution

I encountered such a problem during development A...

Use of Linux cal command

1. Command Introduction The cal (calendar) comman...