欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用C++创建Pyd文件扩展Python模块

程序员文章站 2023-03-31 16:09:29
使用c++创建pyd文件扩展python模块:1、需要说明的是pyd文件其实就是dll,为了python能正常调用,这个dll规定了导出函数的一些规则。下面代码简单演示使用c++创建一个函数,并使用...

使用c++创建pyd文件扩展python模块:1、需要说明的是pyd文件其实就是dll,为了python能正常调用,这个dll规定了导出函数的一些规则。下面代码简单演示使用c++创建一个函数,并使用python调用该模块。

(注意需要添加python的head路径和lib路径,具体操作,可见下面的视频链接)

#include 
#include 

//需要绑定的方法
static pyobject* printhello(pyobject *self, pyobject *args)
{
	std::cout << "hello, i am form c++" << std::endl;

	//测试一下使用c++/clr,让python调用
// 	system::windows::forms::form^ testdotnetwindows = gcnew system::windows::forms::form();
// 	testdotnetwindows->showdialog();

	py_incref(py_none);
	return py_none;
}

//描述方法
static pymethoddef methods[] = {
	{"printhellofn", printhello, meth_varargs, "文档字符串"},
	{null, null}
};

//初始化模块
pymodinit_func initprinthello(void)
{
	py_initmodule("printhello", methods);
}

使用C++创建Pyd文件扩展Python模块