导航:首页 > 编程语言 > python3调用c

python3调用c

发布时间:2022-08-22 04:41:29

python调用c语言编译

如何让python调用C和C++代码

安装python后,会有一个chm格式的python手册。要搞明白如何让python调用C/C++代码(也就是写python的 extension),你需要征服手册中的
<<Extending && embedding>>厚厚的一章。在昨天花了一个小时看地头晕脑胀,仍然不知道如何写python的extension后,查阅了一些其他 书籍,最终在<<Python Programming On Win32>>书中找到了教程。
下面记录一下如何在visual studio 2005中,写一段C/C++的MessageBox代码,然后提供后python调用,最后的结果当然是显示一个MessageBox.
1. 首先要明白的是,所谓的python扩展(也就是你提供给python的c/c++代码,不一定是c/c++代码,可以是其他语言写的代码)是一个 dll,并且这个dll放在本机python安装目录下的DLLs目录下(譬如我机器上的路径是:F:\Program Files\Python25\DLLs),假如我们接下来要写的扩展mole名为mb,python调用的代码为: import mb
mb.showMsg("Python's really amazing, I kindda love it!")
python怎么找到我们的mb模块呢?就是上面说的,我们要生成一个mb.dll,然后拷贝到Dlls目录下面,为了区别普通的dll和python专用扩展的dll,我们的 mb.dll修改成mb.pyd(python dll)
2. 搭建环境,我们要使用python提供的c头文件和lib库来进行扩展的开发。 在vs 2005下点击菜单 "工具"->"选项", 打开选项对话框,选择"项目和解决方案->VC++目录", 然后在右边"显示以下内容的目录"得comboBox上选择"包含文件”,添加python的include目录(我的机器上是"F:\Program
Files\Python25\include"),然后选择库文件,添加python的libs目录(我的机器上是"F:\Program Files\Python25\libs")。
既然扩展是一个dll,接下来我们要建立一个“动态链接库”工程,然后开始写代码:
#include <python.h> //python.h是包含python一些定义的头文件,在python的include目录下 /*
我的python版本是2.5, 因为安装python后它没提供debug下的lib库文件,因此你必须生成release版的dll,
想要生成dll版本的,你要到python官网上自己去下载python源代码,当然你可以继续生成release版本的dll,但dll中包含调试信息

⑵ 怎样让Python脚本与C++程序互相调用

二、Python调用C/C++

1、Python调用C动态链接库

Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。
(1)C语言文件:pycall.c

[html] view plain
/***gcc -o libpycall.so -shared -fPIC pycall.c*/
#include <stdio.h>
#include <stdlib.h>
int foo(int a, int b)
{
printf("you input %d and %d\n", a, b);
return a+b;
}
(2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。
(3)Python调用动态库的文件:pycall.py

[html] view plain
import ctypes
ll = ctypes.cdll.LoadLibrary
lib = ll("./libpycall.so")
lib.foo(1, 3)
print '***finish***'
(4)运行结果:

2、Python调用C++(类)动态链接库

需要extern "C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern "C",构建后的动态链接库没有这些函数的符号表。
(1)C++类文件:pycallclass.cpp

[html] view plain
#include <iostream>
using namespace std;

class TestLib
{
public:
void display();
void display(int a);
};
void TestLib::display() {
cout<<"First display"<<endl;
}

void TestLib::display(int a) {
cout<<"Second display:"<<a<<endl;
}
extern "C" {
TestLib obj;
void display() {
obj.display();
}
void display_int() {
obj.display(2);
}
}
(2)g++编译生成动态库libpycall.so:g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp。
(3)Python调用动态库的文件:pycallclass.py

[html] view plain
import ctypes
so = ctypes.cdll.LoadLibrary
lib = so("./libpycallclass.so")
print 'display()'
lib.display()
print 'display(100)'
lib.display_int(100)
(4)运行结果:

3、Python调用C/C++可执行程序
(1)C/C++程序:main.cpp

[html] view plain
#include <iostream>
using namespace std;
int test()
{
int a = 10, b = 5;
return a+b;
}
int main()
{
cout<<"---begin---"<<endl;
int num = test();
cout<<"num="<<num<<endl;
cout<<"---end---"<<endl;
}
(2)编译成二进制可执行文件:g++ -o testmain main.cpp。
(3)Python调用程序:main.py

[html] view plain
import commands
import os
main = "./testmain"
if os.path.exists(main):
rc, out = commands.getstatusoutput(main)
print 'rc = %d, \nout = %s' % (rc, out)

print '*'*10
f = os.popen(main)
data = f.readlines()
f.close()
print data

print '*'*10
os.system(main)
(4)运行结果:

4、扩展Python(C++为Python编写扩展模块)
所有能被整合或导入到其它python脚本的代码,都可以被称为扩展。可以用Python来写扩展,也可以用C和C++之类的编译型的语言来写扩展。Python在设计之初就考虑到要让模块的导入机制足够抽象。抽象到让使用模块的代码无法了解到模块的具体实现细节。Python的可扩展性具有的优点:方便为语言增加新功能、具有可定制性、代码可以实现复用等。
为 Python 创建扩展需要三个主要的步骤:创建应用程序代码、利用样板来包装代码和编译与测试。
(1)创建应用程序代码

[html] view plain
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int fac(int n)
{
if (n < 2) return(1); /* 0! == 1! == 1 */
return (n)*fac(n-1); /* n! == n*(n-1)! */
}

char *reverse(char *s)
{
register char t, /* tmp */
*p = s, /* fwd */
*q = (s + (strlen(s) - 1)); /* bwd */

while (p < q) /* if p < q */
{
t = *p; /* swap & move ptrs */
*p++ = *q;
*q-- = t;
}
return(s);
}

int main()
{
char s[BUFSIZ];
printf("4! == %d\n", fac(4));
printf("8! == %d\n", fac(8));
printf("12! == %d\n", fac(12));
strcpy(s, "abcdef");
printf("reversing 'abcdef', we get '%s'\n", \
reverse(s));
strcpy(s, "madam");
printf("reversing 'madam', we get '%s'\n", \
reverse(s));
return 0;
}
上述代码中有两个函数,一个是递归求阶乘的函数fac();另一个reverse()函数实现了一个简单的字符串反转算法,其主要目的是修改传入的字符串,使其内容完全反转,但不需要申请内存后反着复制的方法。
(2)用样板来包装代码
接口的代码被称为“样板”代码,它是应用程序代码与Python解释器之间进行交互所必不可少的一部分。样板主要分为4步:a、包含Python的头文件;b、为每个模块的每一个函数增加一个型如PyObject* Mole_func()的包装函数;c、为每个模块增加一个型如PyMethodDef MoleMethods[]的数组;d、增加模块初始化函数void initMole()。

⑶ python怎么调用c的main函数

if
__name__=="__main__":
print
'main'
当脚本作为执行脚本时__name__的值为__main__当脚本作为模块时__name__为模块文件名。举个例子,a.py作为执行脚本时__name__的值是__main__。有2个脚本,a.py和b.py,a中引入b,执行a.py时,在b中模块的__name__就是b.py

⑷ python调用c函数

如果有一堆\0,你怎么知道这个指针指向的内容什么时候结束?比如应该读取10个字节,还是100个字节。
最起码要加一个标示指针所指内容长度的参数。

⑸ python 调用c函数里面的函数吗

若你是想调用 c 编写的DLL,可以使用ctypes调入使用;

#!/usr/bin/python
fromctypesimport*
importos
#需要使用绝对路径
extest=cdll.LoadLibrary(os.getcwd()+'/DemoC.so')

或在windows下

#!/usr/bin/python
importctypes
importos

ifos.name=='nt':#windows系统
_lib_name=os.getcwd()+'/DemoC.DLL'
dl200_lib=ctypes.WinDLL(dl200_lib_name)

⑹ python 怎么调用c语言接口

ctypes: 可直接调用c语言动态链接库。

使用步骤:

1> 编译好自己的动态连接库
2> 利用ctypes载入动态连接库
3> 用ctype调用C函数接口时,需要将python变量类型做转换后才能作为函数参数,转换原则见下图:

#Step1:test.c#include<stdio.h>

intadd(inta,intb)
{
returna+b;
}#Step2:编译动态链接库(如何编译动态链接库在本文不详解,网上资料一大堆。)gcc-fPIC-sharedtest.c-olibtest.so
#Step3:test.py
fromctypesimport*mylib=CDLL("libtest.so")或者cdll.LoadLibrary("libtest.so")add=mylib.add
add.argtypes=[c_int,c_int]#参数类型,两个int(c_int是ctypes类型,见上表)
add.restype=c_int#返回值类型,int(c_int是ctypes类型,见上表)
sum=add(3,6)

⑺ 如何让python调用C和C++代码

二、Python调用C/C++1、Python调用C动态链接库Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。(1)C语言文件:pycall.c[html]viewplain/***gcc-olibpycall.so-shared-fPICpycall.c*/#include#includeintfoo(inta,intb){printf("youinput%dand%d\n",a,b);returna+b;}(2)gcc编译生成动态库libpycall.so:gcc-olibpycall.so-shared-fPICpycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern"C"来进行编译。(3)Python调用动态库的文件:pycall.py[html]viewplainimportctypesll=ctypes.cdll.LoadLibrarylib=ll("./libpycall.so")lib.foo(1,3)print'***finish***'(4)运行结果:2、Python调用C++(类)动态链接库需要extern"C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern"C",构建后的动态链接库没有这些函数的符号表。(1)C++类文件:pycallclass.cpp[html]viewplain#includeusingnamespacestd;classTestLib{public:voiddisplay();voiddisplay(inta);};voidTestLib::display(){cout#include#includeintfac(intn){if(n<2)return(1);/*0!==1!==1*/return(n)*fac(n-1);/*n!==n*(n-1)!*/}char*reverse(char*s){registerchart,/*tmp*/*p=s,/*fwd*/*q=(s+(strlen(s)-1));/*bwd*/while(p

⑻ 如何用python 调用c写的驱动

我觉得会受到限制的,因为c_char_p是遵循c字符串标准的,会以NULL为结束。下面的代码只输出hello,也许真要传递内嵌NULL的,只能靠编写python扩展了,也很简单的,用swig。
from
ctypes
import
*
import
struct
example=cdll.LoadLibrary("example.dll")
s=create_string_buffer('hello\x00world')
example.test.restype=c_char_p
example.test.argtypes
=
[c_char_p]
r=example.test(s)
#("hello\x00world")
print
r

⑼ 如何在python中调用C语言代码

先把C语言代码做成DLL文件,再用python加载此DLL文件来访问C语言功能代码

⑽ google python3能运行.c文件吗

如果是.c文件编译好的可执行文件,可以通过os.popen(filepath)来执行
如果是c编译好的so或dll文件,也可以通过ctypes模块来调用实现,详见https://docs.python.org/3/library/ctypes.html

阅读全文

与python3调用c相关的资料

热点内容
如何共享服务器的网络连接 浏览:130
程序员简易表白代码 浏览:163
什么是无线加密狗 浏览:60
国家反诈中心app为什么会弹出 浏览:64
cad压缩图打印 浏览:100
网页打开速度与服务器有什么关系 浏览:860
android开发技术文档 浏览:62
32单片机写程序 浏览:43
三星双清无命令 浏览:835
汉寿小程序源码 浏览:340
易助erp云服务器 浏览:530
修改本地账户管理员文件夹 浏览:416
python爬虫工程师招聘 浏览:283
小鹏p7听音乐哪个app好 浏览:354
linux下的防火墙 浏览:954
凌达压缩机美芝压缩机 浏览:350
php后面代码不执行 浏览:236
微我手机怎样设置应用加密 浏览:203
条件加密 浏览:630
androidstudio设置中文 浏览:643