python调用C++,使用pybind11

一.准备工作:
1.安装python环境
(如果编译的是32位的python库,则需要安装32位的python环境,
如果编译64位的则安装64位环境)
2.安装环境后敲打命令pip install pytest下载pytest,
敲打命令pip install pybind11下载pybind11
二.创建项目,配置cmake引入python和pybind11相关库
如下面cmake所看到的,我安装了python的32位环境和64位环境,我项目是用的mingw-32进行编译的,
所以我在引入python相关库和头文件的时候选择直接进行手动设置32位python路径

cmake_minimum_required(VERSION 3.5)

project(PythonUseC32 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Core REQUIRED)

#选择32位python
set(Python_ROOT_DIR "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python313-32")
set(Python_EXECUTABLE "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python313-32/python.exe")
set(Python_LIBRARY_DIRS "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python313-32/libs")
set(Python_INCLUDE_DIRS "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python313-32/include")

#选择64位python
#set(Python_ROOT_DIR "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python312")
#set(Python_EXECUTABLE "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python312/python.exe")
#set(Python_LIBRARY_DIRS "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python312/libs")
#set(Python_INCLUDE_DIRS "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python312/include")

find_package(Python COMPONENTS Interpreter Development REQUIRED)

#选择32位pybind11
set(pybind11_DIR "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python313-32/Lib/site-packages/pybind11/share/cmake/pybind11")
#选择64位pybind11
#set(pybind11_DIR "C:/Users/HUAWEI/AppData/Local/Programs/Python/Python312/Lib/site-packages/pybind11/share/cmake/pybind11")
find_package(pybind11 REQUIRED)

pybind11_add_module(PythonUseC32
  PythonUseC32_global.h
  PythonUseC32.cpp
  PythonUseC32.h
)

set_target_properties(PythonUseC32 PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/custom_output_dir"
  LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/custom_output_dir"
)

target_include_directories(PythonUseC32 PRIVATE)

target_link_libraries(PythonUseC32 PRIVATE Qt5::Core pybind11::embed pybind11::module)

target_compile_definitions(PythonUseC32 PRIVATE PYTHONUSEC32_LIBRARY)
三.使用mingw-32和mingw-64编译该项目需要注意的事项
使用mingw-32编译后会生成一个python动态库PythonUseC32.cp313-win32.pyd,python可直接引入该库进行
使用,但是如果用mingw-32和mingw-64编译的库话是使用不了的,所以必须在编译之前进行以下操作。

对于mingw-32编译的python库的话,找到D:\Qt-5.12.8\Tools\mingw730_32\bin文件夹,把其里面所有的.dll文件拷贝到C:\Windows\SysWOW64目录下

对于mingw-64编译的python库的话,找到D:\Qt-5.12.8\Tools\mingw730_64\bin文件夹,把其里面所有
的.dll文件拷贝到C:\Windows\System32目录下

这样使用python动态库的时候就可以使用了
四.python代码编写

import sys
sys.path.append('./custom_output_dir')
import PythonUseC32

result_1 = PythonUseC32.add(3,4)
print(result_1)
result_2 = PythonUseC32.sub(3,4)
print (result_2)

obj = PythonUseC32.PythonUseC32()
obj.hello()
五.C++代码编写

**************************************************

PythonUseC32.h

#ifndef PYTHONUSEC32_H
#define PYTHONUSEC32_H

#include "PythonUseC32_global.h"
#include 
namespace py = pybind11;

class PythonUseC32
{
public:
    PythonUseC32();
    void hello();
};

int add(int i, int j);
int sub(int i, int j);

#endif // PYTHONUSEC32_H

*************************************************

PythonUseC32.cpp

#include "PythonUseC32.h"
#include 

PythonUseC32::PythonUseC32()
{
}

void PythonUseC32::hello()
{
    std::cout << "hello 2025" << std::endl;
}

int add(int i, int j)
{
    return i + j;
}

int sub(int i, int j)
{
    return i *j;
}

// 使用 PYBIND11_MODULE 宏来创建 Python 绑定
PYBIND11_MODULE(PythonUseC32, m) {
    m.doc() = "pybind11 example plugin"; // 模块的文档字符串
    m.def("add", &add, "A function which adds two numbers");
    m.def("sub", &sub, "A function which subtracts two numbers");
    py::class_(m, "PythonUseC32")
    .def(py::init<>())
    .def("hello", &PythonUseC32::hello,"A function say hello");

}

你可能感兴趣的:(Qt,python,开发语言)