歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python使用ctype調用C鏈接庫

Python使用ctype調用C鏈接庫

日期:2017/3/1 10:29:41   编辑:Linux編程

相對於傳統的C調用(見 http://www.linuxidc.com/Linux/2012-02/55037.htm),使用ctype實在是太簡單了

編寫一個動態鏈接庫ctype_test.c,

  1. #include <stdlib.h>
  2. int foo(int a, int b)
  3. {
  4. printf("Your input %i and %i\n", a, b);
  5. return a + b;
  6. }

編譯

gcc -o ctype.so -shared -fPIC ctype_test.c

在python下試用一下吧

  1. import ctypes
  2. ll = ctypes.cdll.LoadLibrary # 我這是在linux下,windows調用windll之類的
  3. lib = ll("./ctype.so")
  4. lib.foo(1, 3)
Copyright © Linux教程網 All Rights Reserved