Python SIP CPP Demo
Install SIP
Download corresponding sip package from here
Create and edit C file add.c
/* File : add.c */
int add(int x, int y)
{
int g;
g = x + y;
return g;
}
Create and edit SIP file add.sip
/* add.sip */
/* Define the SIP wrapper to the add library. */
%Module(name=add, language="C")
int add(int x, int y);
Compile C file to .a
or .so
library
# For .a library
gcc -c add.c
ar -r libadd.a add.o
# For .so library
gcc add.c -shared -fPIC -o libadd.so
Create and edit python file configure.py
import os
import sipconfig
# The name of the SIP build file generated by SIP and used by the build
# system.
build_file = "add.sbf"
# Get the SIP configuration information.
config = sipconfig.Configuration()
# Run SIP to generate the code.
os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "add.sip"]))
# Create the Makefile.
makefile = sipconfig.SIPModuleMakefile(config, build_file)
# Add the library we are wrapping. The name doesn't include any platform
# specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the
# ".dll" extension on Windows).
makefile.extra_libs = ["add"]
# Generate the Makefile itself.
Generate file add.sbf
and Makefile
python3 configure.py
- Note: the
Makefile
should be modified for adaptdynamic
andstatic
library - For
static
library, we should setLIBS
variable to full name of the library. - For
dynamic
library, we shoud add-L .
at the front of the-ladd
toLIBS
variable
Compile and test
- For compile:
make sudo make install
- for test, we just test it in python shell.
import add add.add(2,3)
- Done.