当前位置: 首页 > news >正文

企业网站开发主要职责网页游戏

企业网站开发主要职责,网页游戏,做的网站怎么查看点击率,快速搭建网站2020前言 在vs2019下使用C与Python进行混合编程,在根源上讲,Python 本身就是一个C库,那么这里使用其中最简单的一种方法是把Python的C API来嵌入C项目中,来实现混合编程。当前的环境是,win10,IDE是vs2019,python版本是3.9&#xff0c…

前言

  1. 在vs2019下使用C++与Python进行混合编程,在根源上讲,Python 本身就是一个C库,那么这里使用其中最简单的一种方法是把Python的C API来嵌入C++项目中,来实现混合编程。
  2. 当前的环境是,win10,IDE是vs2019,python版本是3.9,python的环境是使用Anacond安装的。

一、环境配置

1. 安装Python
首先要安装好Python的库,Python可以直接从官网下载,或者直接在conda里面进行安装。

2.添加环境变量
安装完成之后,添加两个系统环境变量,分别是:PYTHONHOME和PYTHONPATH。
在这里插入图片描述
如果不添加这两个系统环境变量会报以下的错误:

Python path configuration:PYTHONHOME = (not set)PYTHONPATH = (not set)program name = 'python'isolated = 0environment = 1user site = 1import site = 1sys._base_executable = 'C:\\code\\cpp\\PDFToDoc\\x64\\Release\\PDFToDoc.exe'sys.base_prefix = 'C:\\Users\\duole\\anaconda3'sys.base_exec_prefix = 'C:\\Users\\duole\\anaconda3'sys.platlibdir = 'lib'sys.executable = 'C:\\code\\cpp\\PDFToDoc\\x64\\Release\\PDFToDoc.exe'sys.prefix = 'C:\\Users\\duole\\anaconda3'sys.exec_prefix = 'C:\\Users\\duole\\anaconda3'sys.path = ['C:\\Users\\duole\\anaconda3\\python39.zip','.\\DLLs','.\\lib','C:\\code\\cpp\\PDFToDoc\\x64\\Release',]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'Current thread 0x000042d4 (most recent call first):
<no Python frame>

3. 创建项目
打开vs2019,创建一个空的新C++项目:
在这里插入图片描述
创建完成后打开项目属于配置包含目录与库目录:
在这里插入图片描述
在附加依赖项目里把python的lib库名添加到里面:
在这里插入图片描述
4.添加代码
在项目里面新添一个main.cpp
在这里插入图片描述
main.cpp里面的代码:

#include <Python.h>int main()
{Py_Initialize();    // 初始化python解释器PyRun_SimpleString("print('hello python')");Py_Finalize();      // 释放资源return 0;
}

然后运行项目
在这里插入图片描述
这样配置就算法成功了。

二、Python C API 调用

为了方便项目测试,在项目根目录下添加一个script目录,在script目录里面创建一个call_python.py的文件。
在这里插入图片描述

2.1 调用Python代码无参函数

C++调用python无参函数流程:

  1. 初始化python接口(Py_Initialize)
  2. 导入依赖库 (PyRun_SimpleString)
  3. 初始化python系统文件路径(PyRun_SimpleString)
  4. 调用python文件名(PyImport_ImportModule)
  5. 获取函数对象(PyObject_GetAttrString)
  6. 调用函数对象(PyObject_CallObject)
  7. 结束python接口调用,释放资源(Py_Finalize)

在call_python.py里面添加代码:

def test():print("hello python to C++")

然后在main.cpp里面进行调用:

int main()
{//1.初始化python接口Py_Initialize();if (!Py_IsInitialized){std::cout << "python init failed" << std::endl;return 1;}//2.导入依赖库PyRun_SimpleString("import sys");//执行py单条语句//3.初始化python系统文件路径,以便访问到python源码文件所在的路径PyRun_SimpleString("sys.path.append('./script')");//4.调用python源码文件,只写文件名,不用写后缀PyObject* module = PyImport_ImportModule("call_python");if (module == nullptr){std::cout << "module not found: call_python" << std::endl;return 1;}//5.获取python文件里面的函数PyObject* test = PyObject_GetAttrString(module, "test");if (!test || !PyCallable_Check(test)){std::cout << "function not found: test" << std::endl;return 1;}//6.调用函数,函数对象与传入参数PyObject_CallObject(test, nullptr);Py_Finalize();return 0;
}

2.2 调用Python代码有参与有返回值的函数

C++调用python有参并有返回的函数流程:

  1. 初始化python接口(Py_Initialize)
  2. 导入依赖库 (PyRun_SimpleString)
  3. 初始化python系统文件路径(PyRun_SimpleString)
  4. 调用python文件名(PyImport_ImportModule)
  5. 获取函数对象(PyObject_GetAttrString)
  6. 传递参数(PyTuple_New,Py_BuildValue)
  7. 调用函数对象(PyObject_CallObject)
  8. 接收函数返回值(PyArg_Parse)
  9. 结束python接口初始化(Py_Finalize)

在call_python.py里面添加代码:

def add(a, b):c = a + bprint(f"{a} + {b} = {c}")return c

然后在main.cpp里面进行调用:

#include <iostream>
#include <Python.h>int main()
{// 1、初始化python接口Py_Initialize();if (!Py_IsInitialized()){std::cout << "python init failed" << std::endl;return 1;}// 2、初始化python系统文件路径,保证可以访问到 .py文件PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append('./script')");// 3、调用python文件名,不用写后缀PyObject* module = PyImport_ImportModule("call_python");if (module == nullptr){std::cout << "module not found: call_python" << std::endl;return 1;}// 4、调用函数PyObject* func = PyObject_GetAttrString(module, "add");if (!func || !PyCallable_Check(func)){std::cout << "function not found: add" << std::endl;return 1;}// 5、给 python 传递参数// 函数调用的参数传递均是以元组的形式打包的, 2表示参数个数// 如果函数中只有一个参数时,写1就可以了PyObject* args = PyTuple_New(2);// 0:第一个参数,传入 int 类型的值 1PyTuple_SetItem(args, 0, Py_BuildValue("i", 1));// 1:第二个参数,传入 int 类型的值 2PyTuple_SetItem(args, 1, Py_BuildValue("i", 2));// 6、使用C++的python接口调用该函数PyObject* ret = PyObject_CallObject(func, args);// 7、接收python计算好的返回值int result;// i表示转换成int型变量。// 在这里,最需要注意的是:PyArg_Parse的最后一个参数,必须加上“&”符号PyArg_Parse(ret, "i", &result);std::cout << "return is " << result << std::endl;// 8、结束python接口初始化Py_Finalize();return 0;
}

2.3 调用Python代码类

C++调用python类流程:

  1. 初始化python接口(Py_Initialize)
  2. 初始化python系统文件路径(PyRun_SimpleString)
  3. 调用python文件名(PyImport_ImportModule)
  4. 获取类(PyObject_GetAttrString)
  5. 根据类构造函数实例化对象(PyEval_CallObject)
  6. 获取实例的函数对象(PyObject_GetAttrString)
  7. 传递参数(PyTuple_New,Py_BuildValue)
  8. 调用函数对象(PyObject_CallObject)
  9. 接收函数返回值(PyArg_Parse)
  10. 结束python接口初始化(Py_Finalize)

在call_python.py里面添加代码:

class Person:def __init__(self, name, age):self.name = nameself.age = agedef foo(self):print(f"my name is {self.name}, my age is {self.age}")

然后在main.cpp里面进行调用:

#include <iostream>
#include <Python.h>int main()
{// 1、初始化python接口Py_Initialize();if (!Py_IsInitialized()){std::cout << "python init failed" << std::endl;return 1;}// 2、初始化python系统文件路径,保证可以访问到 .py文件PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append('./script')");// 3、调用python文件名,不用写后缀PyObject* module = PyImport_ImportModule("call_python");if (module == nullptr){std::cout << "module not found: call_python" << std::endl;return 1;}// 4、获取类PyObject* cls = PyObject_GetAttrString(module, "Person");if (!cls){std::cout << "class not found: Person" << std::endl;return 1;}// 5、给类构造函数传递参数// 函数调用的参数传递均是以元组的形式打包的, 2表示参数个数// 如果函数中只有一个参数时,写1就可以了PyObject* args = PyTuple_New(2);// 0:第一个参数,传入 int 类型的值 1PyTuple_SetItem(args, 0, Py_BuildValue("s", "jack"));// 1:第二个参数,传入 int 类型的值 2PyTuple_SetItem(args, 1, Py_BuildValue("i", 18));// 6、根据类名实例化对象PyObject* obj = PyObject_CallObject(cls, args);// 7、根据对象得到成员函数PyObject* func = PyObject_GetAttrString(obj, "foo");if (!func || !PyCallable_Check(func)){std::cout << "function not found: foo" << std::endl;return 1;}// 8、使用C++的python接口调用该函数PyObject_CallObject(func, nullptr);// 9、结束python接口初始化Py_Finalize();return 0;
}
http://www.tj-hxxt.cn/news/13796.html

相关文章:

  • 网页策划方案seo搜索规则
  • 做网站的公司如何推广seo基础优化包括哪些内容
  • 郑州 网站建设的公司适合小学生的新闻事件
  • 网站建设制作公司知道万维科技软件推广
  • 网站设计如何在ps先做杭州seo推广公司
  • 怎么测试网站华为手机软文范文300
  • 广州做鞋的网站宣传链接怎么做
  • 高端网站建设企业网络营销策划
  • 运营 网站怎么在百度上做公司网页
  • wordpress开发企业网站百度文库网页版登录入口
  • 长沙本地烟台州关键词优化推荐
  • 外贸网站模板 免费深圳网站建设专业乐云seo
  • 晋江网站建设公司人工智能培训机构排名前十
  • wordpress数据库错误惠州seo网站排名
  • 微网站公司百度推广开户费
  • 巫溪集团网站建设搜索引擎yandex入口
  • 深圳做网站外包公司网络seo推广
  • 咸阳 网站建设百度快照怎么发布
  • 网站站群建设方案百度广告安装入口
  • 用fullpage做的网站网站推广专家十年乐云seo
  • 广州建网站模板百度一下百度
  • wordpress可以自动同步吗对seo的认识和理解
  • 网站建设手机端百度推广平台首页
  • 驻马店公司做网站上海互联网公司排名
  • 做公司永久免费网站什么好河北百度推广seo
  • 江西南昌网站定制石家庄关键词排名首页
  • 纺织品公司网站建设电商网站建设平台
  • 广西梧州市住房和城乡建设局网站广告联盟怎么加入
  • 企业注册网站专业网站seo推广
  • 摄影网站设计图片百家号seo