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

XHTML Getting Started Tutorial: Form Tags

<br />Forms are an important channel for use...

HTML table markup tutorial (15): table title

<br />This tag can be used to directly add a...

The Complete List of MIME Types

What is MIME TYPE? 1. First, we need to understan...

CSS Skills Collection - Classics among Classics

Remove the dotted box on the link Copy code The co...

Linux touch command usage examples

Detailed explanation of linux touch command: 1. C...

Detailed example of mysql similar to oracle rownum writing

Rownum is a unique way of writing in Oracle. In O...

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

MySql Sql optimization tips sharing

One day I found that the execution speed of a SQL...

HTML Web Page List Tags Learning Tutorial

HTML web page list tag learning tutorial. In HTML ...

Comprehensive understanding of Node event loop

Table of contents Node Event Loop Event loop diag...

How to use vw+rem for mobile layout

Are you still using rem flexible layout? Does it ...

Detailed tutorial on installing Mysql5.7.19 on Centos7 under Linux

1. Download MySQL URL: https://dev.mysql.com/down...