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. LwIP协议栈(2):网络接口

    在LwIP中,物理网络硬件接口结构保存在一个全局链表中,它们通过结构体中的 next 指针连接. struct netif { /// pointer to next in linked list * ...

  2. boost中全局命名锁的使用

    使用头文件相对位置为:boost/interprocess/sync/named_mutex.hpp 在程序中使用 boost::interprocess::named_mutex g_namedmu ...

  3. java json字符串和对象互转

    /** * Created by admin on 2017/7/26. */ public class NewPost { private String title; private String ...

  4. Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed

    错误原因:常量.随机或者依赖时区的表达式不能作为分区函数. 解决方法:把ts列换成datetime类型,创建成功. CREATE TABLE T_log( id INT(11) NOT NULL AU ...

  5. gcc使用备忘

    本文为原创文章,转载请指明该文链接 Options Controling the kind of Output -x language 明确说明输入文件的编码语言,没有该选项的话, gcc 会根据输入 ...

  6. 【转】Monkey测试4——Monkey命令行可用的全部选项

    Monkey命令行可用的全部选项 常规 --help 列出简单的用法. -v 命令行的每一个-v将增加反馈信息的级别. Level 0(缺省值)除启动提示.测试完成和最终结果之外,提供较少信息. Le ...

  7. spark 1.3 发布了

    悄悄地,spark 还是像往常一样,发布了1.3版本,从release notes可以看出,这一版本比较大的变化是1. 增加了DataFrame API,这样以后操作一些结构化的数据集时将会变的非常方 ...

  8. 简单熟悉eclipse

  9. Unity3D学习笔记——NGUI之UIScrollBar

    UIScrollBar:这个组件可以用于创建滚动条. 效果图如下: 一:使用步骤 1.这个组件和UISlider很像,也是由三部分组成. 2.首先创建一个Sprite用于组件的背景色. 3.创建第二个 ...

  10. Zookeeper权限管理与Quota管理

    Zookeeper的ACL机制和Quota机制网上资料较少,这里做一个总结,以供大家参考. 1 Zookeeper ACL ZooKeeper的权限管理亦即ACL控制功能通过Server.Client ...