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文件流程的更多相关文章

  1. linux下python调用.so文件

    前言 使用python 调用Fanuc的动态链路库.so 文件读取数据 环境要求 环境 需求 ubuntu16.04 32位 python3.5 32位 配置 把so文件添加到默认路径 ln -s / ...

  2. windows下python检查文件是否被其它文件打开

    windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...

  3. Python Windows下打包成exe文件

    Python Windows 下打包成exe文件,使用PyInstaller 软件环境: 1.OS:Win10 64 位 2.Python 3.7 3.安装PyInstaller 先检查是否已安装Py ...

  4. windows下python web开发环境的搭建

    windows下python web开发环境: python2.7,django1.5.1,eclipse4.3.2,pydev3.4.1 一. python环境安装 https://www.pyth ...

  5. [转]Windows下Python多版本共存

    https://blog.csdn.net/dream_an/article/details/51248736 Windows下Python多版本共存 Python数据科学安装Numby,pandas ...

  6. Windows下Python多版本共存

    Windows下Python多版本共存 Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 0.0 因为公司项目,需要Python两个 ...

  7. Windows下python的配置

    Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...

  8. Windows下Python读取GRIB数据

    之前写了一篇<基于Python的GRIB数据可视化>的文章,好多博友在评论里问我Windows系统下如何读取GRIB数据,在这里我做一下说明. 一.在Windows下Python为什么无法 ...

  9. Windows下Python安装numpy+mkl,Scipy和statsmodels

    最近做时间序列分析需要用到Python中的statsmodels,但是安装过程中遇到很头疼的问题,Google.Stackover各种都没有找到合适的解决办法,而且貌似还有很多同学也在吐槽Window ...

随机推荐

  1. MII_GMII_RGMII_RMII_SMII_SSMII_TBI_RTBI比较

    MII_GMII_RGMII_RMII_SMII_SSMII_TBI_RTBI比较 https://wenku.baidu.com/view/2e136caa8bd63186bdebbc40.html

  2. 严重: Error in dependencyCheck java.io.IOException: invalid header field(tomcat启动成功可是訪问web项目404错误)

    tomcat启动的时候出现 严重: Error in dependencyCheck java.io.IOException: invalid header field 而且tomcat也不自己主动r ...

  3. Android学习之ItemTouchHelper实现RecylerView的拖拽以及滑动删除功能

    今天在群里见大神们提到控件的拖动以及滑动删除的效果实现,就在网上找了资料ItemTouchHelper学习,并实现其功能.不胜窃喜之至,忍不住跟大家分享一下,如今就对学习过程做下简介.帮助大家实现这样 ...

  4. 当synchronized关键字和this关键字

    package cn.itcast_01_mythread.thread.testThread; public class MyThreadWithImpliment_Synch_method imp ...

  5. 配置LANMP环境(2)-- 安装ifconfig命令与安装SecureCRT

    一.安装ifconfig命令 yum whatprovides ifconfig yum install net-tools 安装这个命令就是为了查看虚拟机的ip地址,SecureCRT连接必须要ip ...

  6. 个人博客开发之 ueditor 安装

  7. spring oauth Role and Authority and scope

    使用hasRole class Grant implements GrantedAuthority{ @Override public String getAuthority() { return & ...

  8. Large repunit factors (Project Euler 132)

    题目大意: 求出 大数111111.....1 (1e9个1)  前40个质因子的和. 思路:可以把原来的数表示成$\frac{10^k - 1}{9}$ 其中$k=10^9$ 如果一个质数$p$ 满 ...

  9. 寒城攻略:Listo 教你用Swift 语言编写 IOS 平台流媒体播放器

    先展示播放器效果:   依然继承 Listo 本人的强迫症,还是从最初到完毕完整的写一个攻略来记录一下,这里声明 Listo 本人也是看了非常多的戴维营攻略才总结分享给大家这一篇攻略的. 首先,Lis ...

  10. hdu 4005(边双连通)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...