/********************************************************************* * Author:samson * date:02/02/2015 * Test PL Atform: * 3.13.0-24-generic * GNU bash, 4.3.11 (1)-release * *********************************** ********************************/
In the HelloWorld project, a simple two-sum program was compiled, and after compiling it into a shared library, how would you use Python to invoke it?
use the LL command to list the shared library under the current directory, where the shared library is named libhelloworld.so.0.0.0
[Email protected]:~/helloworld/.libs$ ll
Total Dosage 32
Drwxr-xr-x 2 UFO UFO 4096 January 29 14:54.
Drwxr-xr-x 6 UFO UFO 4096 January 29 16:08. /
-rw-r--r--1 UFO UFO 3816 January 14:54 HELLOWORLD.O
-rw-r--r--1 UFO UFO 3956 January 14:54 libhelloworld.a
lrwxrwxrwx 1 UFO UFO 19 January 14:54 libhelloworld.la. /libhelloworld.la
-rw-r--r--1 UFO UFO 983 January 14:54 Libhelloworld.lai
lrwxrwxrwx 1 UFO UFO 22 January 14:54 libhelloworld.so-libhelloworld.so.0.0.0*
lrwxrwxrwx 1 UFO UFO 22 January 14:54 libhelloworld.so.0-libhelloworld.so.0.0.0*
-rwxr-xr-x 1 UFO UFO 9038 January 14:54 libhelloworld.so.0.0.0*
Enter the command-line mode of Python for the C-language implementation of the two value added program of the call;
[Email protected]:~/helloworld/.libs$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:56)
[GCC 4.7.3] on linux2
Type "Help", "copyright", "credits" or "license" for more information.
Load the cTYPES class (this class is the method that calls the C-language dynamic library)
>>> Import cTYPES
open a dynamic library of the current directory
>>> lib=ctypes.cdll.loadlibrary ("./libhelloworld.so.0.0.0")calling interfaces in a dynamic library
>>> Lib.add (5,7)
A
The function of adding two parameters is as follows:
[Email protected]:~/helloworld$ cat HELLOWORLD.C
#include <stdio.h>
#include <stdlib.h>
int add (int a, int b)
{
int C = a + B;
return C;
}
to compile a dynamic library's command line:
gcc-shared-fpic-dpic helloworld.c-o libhelloworld.so.0.0.0
REF:
Http://tool.oschina.net/uploads/apidocs/python2.7.3/library/ctypes.html
The creation of a simple C shared library and methods for Python to call this library