PySide Binding Generation Tutorial: Module 2 Binding libfoo using Shiboken

From Qt Wiki
Jump to: navigation, search

English French

Binding libfoo using Shiboken

In order to create bindings for a library based on Qt, a number of components must be available on the system:

  • Qt library (with headers and pkg-config .pc files for development – the -dev packages in a Debian-like distribution).
  • Qt Python bindings made with Shiboken, i.e. PySide.
  • Type systems for the Qt Python bindings.
  • Headers for the library to be wrapped.

With the items listed above the developer must collect information used by the generator to create the binding source code:

  • Type system file describing the way the binding must be done.
  • global.h including all the libfoo headers and defining required macros.
  • A build system to direct the process of generating, compiling and linking the binding.

libfoo bindings with Makefile

The directory structure and contents for the Makefile binding project could be something like the tree shown below:

|— foo/
|— global.h
|— Makefile
|— pyside_global.h
`— typesystem_foo.xml

The foobinding-makefile directory should contain the global.h, which is a central point to include all the libfoo headers (ok, it's just one header, but a real life library will certainly have more headers), it could also contain #define clauses that will influence the parsing of the headers by the generator. Nevermind the pyside_global.h file, it'll be explained later. The typesystem_foo.xml describes how to export the wrapped C++ library to Python as explained in Module 3.

The foo directory is the place where the generated sources will be placed. It starts empty and its name will be the same as the package name found in the type system file:

<typesystem package="foo">

If there is any need for handwritten source code longer than a couple of lines, making it inconvenient to put them in the type system xml file, the sources could be orderly placed in a glue directory. For the foo bindings no custom code will be needed.

When writing the type system file (more on this later) there is no need to refer to the other required type system files with absolute paths, the locations where they can be found could be passed to the generator through a command line option (—typesystem-paths=PATH1:PATH2:[…]) or the environment variable TYPESYSTEMPATH.

libfoo bindings with CMake

The directory structure for the CMake based libfoo bindings is a bit more elaborate:

|— CMakeLists.txt
|— foo/
| |— CMakeLists.txt
| |— global.h
| |— pyside_global.h
| `— typesystem_foo.xml
`— tests/
 `— CMakeLists.txt