c/c++再学习:C与Python相互调用
c/c++再学习:Python调用C函数
Python 调用C函数比较简单
这里两个例子,一个是直接调用参数,另一个是调用结构体
C代码
typedef struct {
int i1;
int i2;
char str[20];
} core_data_t;
__declspec(dllexport) int add(int a, int b)
{
return a + b;
}
__declspec(dllexport) int multi(int a, int b)
{
return a * b;
}
__declspec(dllexport) int struct_add(core_data_t* data)
{
printf("%s\n", data->str);
return data->i1 + data->i2;
}
python代码
from ctypes import *
core = CDLL('core.dll')
add_val = core.add(1, 2)
multi_val = core.multi(2, 3)
print(add_val)
print(multi_val)
class CoreData(Structure):
_fields_ = [("i1", c_int),
("i2", c_int),
("str", c_char*20)]
coredata = CoreData()
coredata.i1 = 10
coredata.i2 = 20
coredata.str = b"hello world"
coredata_ptr = byref(coredata)
struct_add_val = core.struct_add(coredata_ptr)
print(struct_add_val)
结果
3
6
30
hello world
C调用python函数
c调用python,需要在增加<Python.h>和python36.lib,有时遇到编译时需要python36_d.lib时,只需要将python36.lib复制重命名为python36_d.lib放在同目录下即可
python代码
def py_print():
print("py_print")
def py_add(a,b):
return a+b
c代码
#include "stdio.h"
#include "windows.h"
#include <Python.h>
void main()
{
Py_Initialize();
PyObject* pModule = NULL;
PyObject* pFunc = NULL;
PyObject* pArgs = NULL;
PyObject* pValue = NULL;
pModule = PyImport_ImportModule("python_demo");
pFunc = PyObject_GetAttrString(pModule, "py_print");
PyEval_CallObject(pFunc, NULL);
pFunc = PyObject_GetAttrString(pModule, "py_add");
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 5));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 10));
pValue = PyEval_CallObject(pFunc, pArgs);
int res = 0;
PyArg_Parse(pValue, "i", &res);
printf("res %d\n", res);
Py_Finalize();
return;
}
结果
py_print
res 15
c/c++再学习:C与Python相互调用的更多相关文章
- IronPython C#与Python相互调用
ironphy microsoft.scripting dll using System;using System.Collections.Generic;using System.Linq;usi ...
- python模块--如何相互调用自己写的模块
一.模块相互调用同级目录调用时的两种方法 import module print(module.add(3,8)) from module import add print(add(2,4)) 同级目 ...
- 【学习笔记】PYTHON语言程序设计(北理工 嵩天)
1 Python基本语法元素 1.1 程序设计基本方法 计算机发展历史上最重要的预测法则 摩尔定律:单位面积集成电路上可容纳晶体管数量约2年翻倍 cpu/gpu.内存.硬盘.电子产品价格等都遵 ...
- Noah的学习笔记之Python篇:命令行解析
Noah的学习笔记之Python篇: 1.装饰器 2.函数“可变长参数” 3.命令行解析 注:本文全原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) ...
- Noah的学习笔记之Python篇:装饰器
Noah的学习笔记之Python篇: 1.装饰器 2.函数“可变长参数” 3.命令行解析 注:本文全原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) ...
- Python学习之二:Python 与 C 区别
引自http://www.lxway.com/181844.htm 从开始看Python到现在也有半个多月了,前后看了Python核心编程和Dive into Python两本书.话说半个月看两本,是 ...
- python学习第九讲,python中的数据类型,字符串的使用与介绍
目录 python学习第九讲,python中的数据类型,字符串的使用与介绍 一丶字符串 1.字符串的定义 2.字符串的常见操作 3.字符串操作 len count index操作 4.判断空白字符,判 ...
- Python实例浅谈之三Python与C/C++相互调用
一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不经过 ...
- Android JNI学习(三)——Java与Native相互调用
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
随机推荐
- Borůvka algorithm
Borůvka algorithm 我好无聊啊,直接把wiki的算法介绍翻译一下把. wiki关于Borůvka algorithm的链接:链接 Borůvka algorithm是一个在所有边权都是 ...
- HTTP协议6之状态码--转
HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了. 如果是做AJAX,REST,网络爬虫, ...
- input表单强制大小写
如题,在HTML页面中常常有遇到强制表单大小写的场景. 在css中设置,HTML页面元素引用就可以了 强制大写: .toUp{ text-transform:uppercase; } 强制小写: .t ...
- Angular记录(10)
文档资料 速查表:https://www.angular.cn/guide/cheatsheet 风格指南:https://www.angular.cn/guide/styleguide Angula ...
- 操作系统层面聊聊BIO,NIO和AIO (epoll)
BIO 有了Block的定义,就可以讨论BIO和NIO了.BIO是Blocking IO的意思.在类似于网络中进行read, write, connect一类的系统调用时会被卡住. 举个例子,当用re ...
- JGUI源码:组件及函数封装方法(7)
以Accordion为例1.在base.js定义一个对象,这样可以和JQuery对象区分开,用户使用组件时比较清晰一点,也可以在这里引用多个库. var JGUI = J = { version : ...
- 前端自动提示功能插件-typeahead
typeahead https://npm.taobao.org/package/npm-typeahead A lightweight web-app that implements typeahe ...
- Now you can provide attr "wx:key" for a "wx:for" to improve performance. 微信小程序警告
Now you can provide attr "wx:key" for a "wx:for" to improve performance为警告,不处理不影 ...
- JAVA进阶18
间歇性混吃等死,持续性踌躇满志系列-------------第18天 1.飞机游戏小项目 ①创建窗口 package cn.xfj.game; import javax.swing.*; import ...
- WebRTC Precompiled 使用
最近研究webrtc native code,但源码太大(10GB以上)又需要FQ,就找了个预编译的版本https://sourcey.com/precompiled-webrtc-libraries ...