windows下python调用c文件流程
1.新建fun.c文件和fun.h文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fac(int n)
{
if(n < 2)
return 1;
return n * fac(n-1);
}
char *reverse(char *s)
{
char t, *p = s,*q=(s+(strlen(s)-1));
while(s && (p<q)){
t = *p;
*p++ = *q;
*q-- = t;
}
return s;
}
int test(void)
// int main(void)
{
char s[1024];
printf("4!==%d\n",fac(4));
printf("8!==%d\n",fac(8));
strcpy(s,"arthas");
printf("reversing 'arthas',we get '%s'\n",reverse(s));
return 0;
}
#ifndef FUN_H_
#define FUN_H_
int fac(int n);
char *reverse(char *s);
int test(void);
#endif
2.新建funwrapper.c文件
#include "Python.h"
#include <stdlib.h>
#include <string.h>
#include "fun.h"
static PyObject *fun_fac(PyObject *self,PyObject *args)
{
int num;
if(!PyArg_ParseTuple(args,"i",&num))
return NULL;
return (PyObject*)Py_BuildValue("i",fac(num));
}
static PyObject *fun_reverse(PyObject *self,PyObject *args)
{
char *src;
char *mstr;
PyObject *retval;
if(!PyArg_ParseTuple(args,"s",&src))
return NULL;
mstr = malloc(strlen(src)+1);
strcpy(mstr,src);
reverse(mstr);
retval = (PyObject*)Py_BuildValue("ss",src,mstr);
free(mstr);
return retval;
}
static PyObject *fun_test(PyObject *self,PyObject *args)
{
return (PyObject*)Py_BuildValue("i",test());
}
static PyMethodDef funMethods[] = {
{"fac",fun_fac,METH_VARARGS},
{"reverse",fun_reverse,METH_VARARGS},
{"test",fun_test,METH_VARARGS},
{NULL,NULL},
};
void initfun(void)
{
Py_InitModule("fun",funMethods);
}
3.CMD使用指令
setup.py install build
这里我出错了python Unable to find vcvarsall.bat 错误,
参考上一篇日志python Unable to find vcvarsall.bat 错误解决方法。解决了。
然后生成出fun.pyd文件
4.复制到python的DLLS文件夹下
5.可以用python调用了
import fun
print fun.fac(4)
print fun.reverse("arthas")
fun.test()
6.输出的结果
···
24
('arthas', 'sahtra')
4!24
8!40320
reversing 'arthas',we get 'sahtra'
···
windows下python调用c文件流程的更多相关文章
- linux下python调用.so文件
前言 使用python 调用Fanuc的动态链路库.so 文件读取数据 环境要求 环境 需求 ubuntu16.04 32位 python3.5 32位 配置 把so文件添加到默认路径 ln -s / ...
- windows下python检查文件是否被其它文件打开
windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...
- Python Windows下打包成exe文件
Python Windows 下打包成exe文件,使用PyInstaller 软件环境: 1.OS:Win10 64 位 2.Python 3.7 3.安装PyInstaller 先检查是否已安装Py ...
- windows下python web开发环境的搭建
windows下python web开发环境: python2.7,django1.5.1,eclipse4.3.2,pydev3.4.1 一. python环境安装 https://www.pyth ...
- [转]Windows下Python多版本共存
https://blog.csdn.net/dream_an/article/details/51248736 Windows下Python多版本共存 Python数据科学安装Numby,pandas ...
- Windows下Python多版本共存
Windows下Python多版本共存 Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 0.0 因为公司项目,需要Python两个 ...
- Windows下python的配置
Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...
- Windows下Python读取GRIB数据
之前写了一篇<基于Python的GRIB数据可视化>的文章,好多博友在评论里问我Windows系统下如何读取GRIB数据,在这里我做一下说明. 一.在Windows下Python为什么无法 ...
- Windows下Python安装numpy+mkl,Scipy和statsmodels
最近做时间序列分析需要用到Python中的statsmodels,但是安装过程中遇到很头疼的问题,Google.Stackover各种都没有找到合适的解决办法,而且貌似还有很多同学也在吐槽Window ...
随机推荐
- CentOS系统时间与网络同步
新装的CentOS系统server可能设置了错误的,须要调整时区并调整时间.例如以下是CentOS系统使用NTP来从一个时间server同步: 第一步: 把当前时区调整为上海就是+8区,想改其它时区也 ...
- Virtex6 PCIe 超简版基础概念学习(一)
文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 ise14.7 DBF板 Day2/PCIETest1 2016.03.31 lutianfei none 参考资料: Sparta ...
- docker的使用01
使用Dockerfile构建镜像 vim dockerfile01 #注释信息 FROM ubuntu:latest //导入镜像 MAINTAINER leo "leo@leo.com&q ...
- Cap'n Proto, FlatBuffers, and SBE
转自:utm_source=tuicool">http://kentonv.github.io/capnproto/news/2014-06-17-capnproto-flatbuff ...
- PHP 高精度计算
运行bcsub函数需要 php-bcmath的扩展. Php: BCMath bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如st ...
- html 常用
1. var canvas = document.getElementById("tutorial"); var ctx = canvas.getContext("2d& ...
- linux oracle sqlplus中文乱码解决
在oracle用户的~/.bash_profile中添加 NLS_LANG="SIMPLIFIED CHINESE"_CHINA.ZHS16GBKexport NLS_LANG 然 ...
- 可以将化学结构NMR图谱这样导入Word
在化学各个领域中,大家常常会用到ChemDraw化学绘图软件来绘制各种图形,ChemDraw因其出色的功能在全球范围内深受欢迎,但是一些用户朋友对于一些功能还不是很了解,需要通过一些教程来了解如何操作 ...
- OpenCV学习笔记二:OpenCV模块一览
注:本系列博客基于OpenCV 2.9.0.0 一,一览图: 二,模块: /* 基础库 */ 1,opencv_core(链接) ,opencv最基础的库.包含exception,point,rect ...
- 2、easyUI-创建 CRUD可编辑dataGrid(表格)
在介绍这节之前,我们先看一下效果图: 双击可以进入编辑