Python:sip Compilation and Use

1. Compile sip source code

  • Unzip the sip source and run win32-g++ (win32-g++ means using the mingw32 compiler).
  • Modify generatedSipconfig.pyFile.When Python is installed in the C:\Program Files (x86) directory, place _Pkg_Program Files (x86) in config was replaced with Progra~2, as follows:
_pkg_config = {
    'arch':               '',
    'default_bin_dir':    'C:\\Progra~2\\Python',
    'default_mod_dir':    'C:\\Progra~2\\Python\\Lib\\site-packages',
    'default_sip_dir':    'C:\\Progra~2\\Python\\sip',
    'deployment_target':  '',
    'platform':           'win32-g++',
    'py_conf_inc_dir':    'C:\\Progra~2\\Python\\include',
    'py_inc_dir':         'C:\\Progra~2\\Python\\include',
    'py_lib_dir':         'C:\\Progra~2\\Python\\libs',
    'py_version':         0x030603,
    'qt_framework':       0,
    'sip_bin':            'C:\\Progra~2\\Python\\sip',
    'sip_config_args':    '-p win32-g++',
    'sip_inc_dir':        'C:\\Progra~2\\Python\\include',
    'sip_mod_dir':        'C:\\Progra~2\\Python\\Lib\\site-packages',
    'sip_version':        0x041306,
    'sip_version_str':    '4.19.6',
    'universal':          ''

When Python is installed in the C:\Program Files directory, place _Pkg_Replace Program Files with Progra~1 in config.

  • Compile, run make, and run make install to install sip after success.Where make install requires administrator privileges.

2. Testing

  • Write the C file test.c as follows:
#include <stdio.h>

int add(int x, int y)
    return x + y;

void hello(char* s)
    printf("%s\n", s);

char* toString(int x, float y)
    static char s[20];
    sprintf(s, "x = %d; y = %f", x, y);
    return s;
  • Write SIP fileTest.sip.The SIP file name must be the same as the.c file name.Since you are using the c language, you must add%Module(name=test, language="C").The contents are as follows:
/* Define the SIP wrapper to the add library. */  
%Module(name=test, language="C")
int add(int x, int y);
void hello(char* s);
char* toString(int x, float y);
  • To, as follows:
import os

import sipconfig

basename = "test"

# The name of the SIP build file generated by SIP and used by the build
# system.
build_file = basename + ".sbf"

# Get the SIP configuration information.
config = sipconfig.Configuration()
os.system("make clean")
# Run SIP to generate the code.
os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, basename + ".sip"]))
os.system(" ".join(['gcc', '-c', basename + ".c"]))
os.system(" ".join(['ar', '-r', "lib" + basename + ".a", basename + ".o"]))
# 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 = [basename]
# Search libraries in current directory
makefile.extra_lflags = ['-L.']

# Generate the Makefile itself.

Run PythonConfigure.pyThe resulting file is as follows:


amongTest.pydThis is the compiled file.

  • test
import test

a = test.add(12, 3)
test.hello('How do you do'.encode())
s = test.toString(12, 45.6)

b'x = 12; y = -2.000000'
//How do you do

Tags: Python Makefile Unix Windows

Posted on Mon, 25 May 2020 12:21:04 -0400 by endersix