将c、c++变为python

1.编写cpp文件

复制代码
#include "pycpp.h"
#include <iostream>

using namespace std;

PyCpp::PyCpp(){

}

void PyCpp::sayHello(int a){
    cout << "Hello Python, I am C++."<<a << endl;
}

2.编写头文件(声明变量)

复制代码
class PyCpp{
public:
    PyCpp();
    void sayHello(int a);
};
// 不能定义编译器生成的特殊成员函数(必须首先在类中声明)

3.编写.i文件,用于链接

复制代码
%module pycpp

%{
#include "pycpp.h"
%}

%include "pycpp.h"

4.在同级目录下放python.h(去conda的env里面找)

5.下载swig运行代码
swig -c++ -python 1/pycpp.i

生成_wrap.cxx

6.编写setup.py

复制代码
from distutils.core import setup, Extension
pycpp_module = Extension('_pycpp',
                           sources=['pycpp.cpp', 'pycpp_wrap.cxx',],
                           )
setup (name = 'pycpp',
       version = '0.1',
       author      = "NZACH",
       description = """Simple swig C\+\+/Python example.""",
       ext_modules = [pycpp_module],
       py_modules = ["pycpp"],
       )

7.运行setup.py
python setup.py build_ext --inplace

8.使用

复制代码
>>> import pycpp
>>> pycpp.PyCpp().sayHello(12)
Hello Python, I am C++.12
相关推荐
WSSWWWSSW11 分钟前
Matplotlib数据可视化实战:Matplotlib子图布局与管理入门
python·信息可视化·matplotlib
WSSWWWSSW12 分钟前
Matplotlib数据可视化实战:Matplotlib图表美化与进阶教程
python·信息可视化·matplotlib
mftang16 分钟前
Python可视化工具-Bokeh:动态显示数据
开发语言·python
Seeklike26 分钟前
diffuxers学习--AutoPipeline
人工智能·python·stable diffusion·diffusers
前端小趴菜0533 分钟前
python - 数据类型
python
前端小趴菜051 小时前
python - 变量
python
再吃一根胡萝卜1 小时前
使用 squashmigrations 命令优化 Django 迁移文件
python·django
逆向菜鸟1 小时前
【摧毁比特币】椭圆曲线象限细分求k-陈墨仙
python·算法
有梦想的攻城狮2 小时前
Java 11中的Collections类详解
java·windows·python·java11·collections
前端小趴菜052 小时前
python - input()函数
python