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

如何在 C++ 中调用 python 解析器来执行 python 代码(一)?

实现 Python UDF 中的一步就是学习如何在 C++ 语言中调用 python 解析器。本文根据 Python 官方文档做了一次实验,记录如下:

1. 安装依赖包

$sudo yum install python3-devel.x86_64

2. 使用 python-config 来生成编译选项

$python3.6-config --cflags --ldflags
-I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv   -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv-L/usr/lib64 -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic

3. 编写 Makefile

将第二步生成的编译、链接选项填到 Makefile 中,得到 Makefile 如下:

all:g++ -I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv   -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv  -L/usr/lib64 -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic main.cpp

4. 编写 main.cpp

#define PY_SSIZE_T_CLEAN
#include <Python.h>int
main(int argc, char *argv[])
{wchar_t *program = Py_DecodeLocale(argv[0], NULL);if (program == NULL) {fprintf(stderr, "Fatal error: cannot decode argv[0]\n");exit(1);}Py_SetProgramName(program);  /* optional but recommended */Py_Initialize();PyRun_SimpleString("from time import time,ctime\n""print('Today is', ctime(time()))\n");if (Py_FinalizeEx() < 0) {exit(120);}PyMem_RawFree(program);return 0;
}

5. 编译

$make
g++ -I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv   -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv  -L/usr/lib64 -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic main.cpp

6. 执行

$./a.out
Today is Wed Mar  1 14:23:13 2023

后继章节预告

  1. 如何在脚本片段中使用第三方库
  2. 如何传参到 Python 脚本
  3. 如何处理 Python 脚本的返回值
  4. 并发调用 Python 解析器
  5. 效率讨论

参考文献

Embedding python in C++:https://docs.python.org/3/extending/embedding.html

http://www.lryc.cn/news/25159.html

相关文章:

  • 操作系统权限提升(二十三)之Linux提权-通配符(ws)提权
  • Zookeeper下载和安装
  • Biomod2 (上):物种分布模型预备知识总结
  • 操作指南:如何高效使用Facebook Messenger销售(二)
  • 计算机三级|网络技术|中小型网络系统总体规划与设计方案|IP地址规划技术|2|3
  • 为什么一定要做集成测试?
  • 前端:CSS
  • CMMI—组织级过程定义(OPD)
  • 华为OD机试真题Python实现【猜字谜】真题+解题思路+代码(20222023)
  • 软测入门(三)Selenium(Web自动化测试基础)
  • 备战蓝桥杯——sort函数
  • 华为机试题:HJ86 求最大连续bit数(python)
  • 机器学习复习--logistic回归简单的介绍和代码调用
  • uniapp小程序接入腾讯地图sdk
  • 总结JavaScript中的条件判断与比较运算
  • 算法练习-排序(一)
  • CentOS7.6快速安装Docker
  • CentOS 7安装N卡驱动和CUDA和cuDNN
  • Java开发 - 分页查询初体验
  • C语言循环语句do while和嵌套循环语句讲解
  • 【计算机视觉】OpenCV 4高级编程与项目实战(Python版)【7】:拼接图像
  • 王道操作系统课代表 - 考研计算机 第二章 进程与线程 究极精华总结笔记
  • C++修炼之练气期三层——函数重载
  • 在linux上运行jar程序操作记录
  • 【STM32】入门(十二):实时操作系统RTOS和通用操作系统GPOS的区别
  • 2023-3-1刷题情况
  • Web前端学习:五 - 练习
  • 软件测试之测试用例设计方法—等价类划分法
  • gdb常用命令详解
  • 2022 年江西省职业院校技能大赛高职组“信息安全管理与评估”赛项样题