For python 3:
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
Interface file
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
Building a Python module
D:\temp\Documents\Python_call_C\example>swig -python -py3 example.i
D:\temp\Documents\Python_call_C\example>gcc -c example.c example_wrap.c -I"D:\Program Files (x86)\Python36-32\include"
D:\temp\Documents\Python_call_C\example>gcc -shared example.o example_wrap.o "D:\Program Files (x86)\Python36-32\python36.dll" "C:\Windows\System32\msvcr120.dll" -o _example.pyd
We can now use the Python module:
import example
print(example.fact(5))
print(example.my_mod(7,3))
print(example.get_time())
>>>>>>>>>>>>>>>>>>>>>>
Example Result:
120
1
Sun Aug 26 21:26:01 2018
沒有留言:
張貼留言