PYTHON 與C相互互動呼叫例項解析

2021-04-17 18:17:09 字數 1299 閱讀 8933

使用前工具:

vc++

編譯器

python

直譯器

如沒有裝

vc,可以去微軟**下乙個

裝完後,在環境變數中把

python

的include

和libs

分別加入下面2個巨集

include

lib1、c

中呼叫python

#include

int main(int argc, char *argv)

直接用cl 檔名

編譯就是 2

、用c生成dll,

用python呼叫

c**:如

foo.c

#include

/* define the method table. */

static pyobject *foo_bar(pyobject *self, pyobject *args);

static pymethoddef foomethods = ,

};/* here's the initialization function.  we don't need to do anything

for our own needs, but python needs that method table. */

void initfoo()

/* finally, let's do something ... involved ... as an example function. */

static pyobject *foo_bar(pyobject *self, pyobject *args)

c定義檔案:

foo.def

exports

initfoo

編譯生成

foo.dll

cl -c foo.c;

link foo.obj /dll

/def:foo.def

/out:foo.dll 在

python

中呼叫:

import foo

dir(foo)

…即可以看到結果:

>>> import foo

>>> dir(foo)

['__doc__', '__file__', '__name__', 'bar']

>>> ^z

PYTHON 與C相互互動呼叫例項解析

1 c中呼叫python include int main int argc,char ar 使用前工具 vc 編譯器 python 直譯器 如沒有裝vc,可以去微軟 下乙個c 的編譯器,位址如下 裝完後,在環境變數中把python的include和libs分別加入下面2個巨集 include li...

lua與C 相互呼叫

新建乙個工程,將編譯出來的lua.lib包含進來,就可以開始用了 1 c 呼叫 lua 編寫 如下 include using namespace std pragma comment lib,lua.lib extern c void main int nret lual loadfile l,l...

C與C 之間相互呼叫

c與c 之間相互呼叫 1 匯出c函式以用於c或c 的專案 如果使用c語言編寫的dll,希望從中匯出函式給c或c 的模組訪問,則應使用 cplusplus 預處理器巨集確定正在編譯的語言。如果是從c 語言模組使用,則用c鏈結宣告這些函式。如果使用此技術並為dll提供標頭檔案,則這些函式可以原封不動地由...