wrapping c code in python

*********************************************

bridge between python and c introduction: connect.appen.com/qrp/core/login

*********************************************

wrapping c code in python

1. write c code

#include 
#include 

int list_month(int i){

        switch(i){
                case 1:printf("Jan\n");
                        break;
                case 2:printf("Feb\n");
                        break;
                case 3:printf("Mar\n");
                        break;
                case 4:printf("Apr\n");
                        break;

                default:break;
        }

}

 

2. compile it as shared library

    gcc -shared -Wl,-soname,clib -o clib.so -fPIC clib.c

 

3. wrapping it in python

from ctypes import *

lib=CDLL("/home/tom/workspace/study/c/shared_so/clib.so")
lib.list_month(4)

 

step by step tutorial from: https://stackoverflow.com/questions/5081875/ctypes-beginner

你可能感兴趣的:(software,skill)