日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python调用.a静态库_Python 调用 C

發布時間:2025/3/11 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用.a静态库_Python 调用 C 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

了解了相關資料

不折騰的方法有(以往文章有):

pypy,numba,numpy

但都不是 純正的 C

折騰的:

cffi,Cython,Boost.Python,Cpython 自帶模塊,SWIG 等

挺折騰的You can write an extension yourself in C or C++ with the Python C-API.

In a word: don't do that except for learning how to do it. It's very difficult to do it correctly. You will have to increment and decrement references by hand and write a lot of code just to expose one function, with very few benefits.

于是想到了 dll,編譯生成 dll,再去調用。

比較常用的輕量編譯器是 tcc,可以搜到 python-bindSasView/tinycc?github.com

下面是方法:

test.c

int add(int a, int b)

{

return a + b;

}

main.py

from tinycc import compile

from ctypes import cdll

dll_path = compile("test.c") # dll_path 是生成的 dll 的路徑

my = cdll.LoadLibrary(r"your path to test.dll")

print(my.add(1, 2))

運行 main.py 就完成了編譯 dll + 調用

test.c

#include

int add(int a, int b)

{

return a + b;

}

int sub(int a, int b)

{

return a - b;

}

int len(const char *c)

{

return strlen(c);

}

main.py

from tinycc import compile

from ctypes import cdll

dll_path = compile("test.c")

my = cdll.LoadLibrary(r"path to test.dll")

print(my.add(2 ** 31 + 3, 2), my.sub(3, 6), my.len(b"tssss"))

-2147483643 -3 5

總結

以上是生活随笔為你收集整理的python调用.a静态库_Python 调用 C的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。