C言語で作った共有オブジェクトと連携してみる。
$ cat greeting.c
char *
getMessage (void)
{
return "HELLO!";
}
void
sayHowRU ()
{
printf ("Hi, how are you?\n");
}
int
getSquare (int i)
{
return i * i;
}
int
multiplication (int a, int b)
{
return a * b;
}
$ gcc -c greeting.c && gcc -shared -o greeting.so greeting.o
$ ls -l *.so
-rwxr-xr-x 1 kishi kishi 10638 May 25 19:54 greeting.so
$ cat MyProto.py
#!/usr/bin/env python
import ctypes
myLib = ctypes.CDLL( './greeting.so' )
"""
-- This is the case that the function has no arguments
getMessage = myLib.getMessage
getMessage.argtypes = [ctypes.c_void_p]
getMessage.restype = ctypes.c_char_p
print getMessage(None)
"""
#------------------------------------------------
# calls C function "getMessage()"
#------------------------------------------------
getMessage = myLib.getMessage
getMessage.restype = ctypes.c_char_p
print getMessage()
#------------------------------------------------
# calls C function "sayHello()"
#------------------------------------------------
sayHowRU = myLib.sayHowRU
sayHowRU()
#------------------------------------------------
# calls C function "getSquare()"
#------------------------------------------------
getSquare = myLib.getSquare
print getSquare(10)
#------------------------------------------------
# calls C function "multiplication()"
#------------------------------------------------
multi = myLib.multiplication
print multi(9,9)
$ ./MyProto.py
HELLO!
Hi, how are you?
100
81