Jump to content

Python bindings for SystemC


Recommended Posts


     Has anyone succeeded in generating python bindings for SystemC, using PyBindGen?


I've managed to run through the process, but can't seem to figure out why I can't find any classes or functions inside the scanned submodules (e.g. sc_core).


I've outlined the procedure that I've followed so far, below. It would be great if someone could help me finish it, but figuring out what's missing.




This document describes how to generate Python-2.7 bindings for SystemC-2.3 using PyBindGen.


PyBindGen is a Python bindings generator; it is geared to generating C/C++ code that binds a C/C++ library for Python.


PyBindGen is a Python module that is geared to generating C/C++ code that binds a C/C++ library for Python. It does so without extensive use of either C++ templates or C pre-processor macros. It has modular handling of C/C++ types, and can be extended with Python plugins. The generated code is almost as clean as what a human programmer would write.


It was designed to be an alternative to some other bindings generators out there, including:

    - Boost::Python, which uses C++ templates extensively, which are difficult to understand or extend, and which generates relatively large Python modules;

    - SWIG, which makes extensive use of macros, generates very ugly code, and is itself written in C++, therefore difficult to extend;

    - Python SIP, which is also written in C++;

    - The PyGtk code generator, which doesn't support wrapping C++ code, and is not very flexible;






Step 01.00: Install pre-requisite packages.


Step 01.01: Install SystemC-2.3.0.



Download the systemc library.




Extract systemc-2.3.0.tar.gz to /tool/accelera folder


Configure and build the systemc library.


cd /tool/accelera/systemc-2.3.0

mkdir build; cd build




In the build directory, modify the systemc-2.3.0/build/src/sysc/kernel/Makefile and remove 


am_objects_2 = 

sc_main.lo, sc_main_main.lo



sc_main.cpp \

sc_main_main.cpp \


to avoid issues with unresolved external symbol sc_main, while importing the python module that you will generate, in subsequent steps.



make -j8

make install


The libraries will be installed to the following location by default:





Set the SYSTEMC environment variable to point to the SystemC installation.


export SYSTEMC=/tool/accelera/systemc-2.3.0




export LD_LIBRARY_PATH="${SYSTEMC}/lib-linux64:${LD_LIBRARY_PATH}"



Step 01.02: Install gccxml and pygccxml


sudo apt-get install gccxml python-pygccxml



Step 02.00: Install pybindgen.


Step 02.01: Download pybindgen-0.16.0


Step 02.02: Configure and install pybindgen.


cd /tool/pybindgen-0.16.0

./waf configure


./waf check

sudo ./waf install


Step 02.03: Test pybindgen using the first-example program.


Download the first-example.zip file from




Modify the makefile to create a shared library using ld, instead of gcc





all: MyModule.so


# build my C library

libmymodule.so: my-module.o

ld -shared -o libmymodule.so my-module.o

my-module.o: my-module.h my-module.c

gcc -fPIC -c -o my-module.o my-module.c


# generate the binding code

my-module-binding.c: my-module.h my-module.py

PYTHONPATH=$$PYTHONPATH:$(PYBINDGEN_LOCATION) python my-module.py > my-module-binding.c

# build the binding code

my-module-binding.o: my-module-binding.c

gcc -fPIC -I$(PYTHON_INCLUDE) -c -o my-module-binding.o my-module-binding.c

# build the final python module

MyModule.so: libmymodule.so my-module-binding.o

ld -shared -o MyModule.so -L. -lmymodule my-module-binding.o



rm -f *.o *.so my-module-binding.c *~ 2>/dev/null



Step 03.00: Create a pybindgen script to automatically scan and generate the python systemc bindings.


Step 03.01: Create a pybindgen autoscan module.


Filename: pysystemc-autoscan.py


#! /usr/bin/env python


import sys


from pybindgen import FileCodeSink

from pybindgen.gccxmlparser import ModuleParser


def systemc_gen():

    module_parser = ModuleParser('pysystemc', '::')

    module_parser.parse([sys.argv[1]], includes=['"systemc.h"'], pygen_sink=FileCodeSink(sys.stdout))


if __name__ == '__main__':




Create the generator


python pysystemc-autoscan.py systemc.h > pysystemc-codegen.py


Create the python binding file


python pysystemc-codegen.py systemc.h > pysystemc-binding.cc


Compile the python binding file:


c++ -fPIC -I/usr/include/python2.7 -I/tool/accelera/systemc-2.3.0/src -c -o pysystemc-binding.o pysystemc-binding.cxx


Build the shared library:


c++ -shared -o pysystemc.so -L. -L/tool/accelera/systemc-2.3.0/lib-linux64 pysystemc-binding.o -Wl,--whole-archive /tool/accelera/systemc-2.3.0/lib-linux64/libsystemc.a -Wl,--no-whole-archive


After this, test the installation by typing 




>>> import pysystemc


The sc_core submodule is present in the pysystemc module, but it doesn't have any classes or methods.


Best regards,


Elvis Dowson

Link to comment
Share on other sites

  • 3 months later...
  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...