La estructura de construcción

Generation of the source code for a gtkmm-style wrapper API requires use of tools such as gmmproc and generate_wrap_init.pl, which are included in glibmm. In theory you could write your own build files to use these appropriately, but a much better option is to make use of the build infrastructure provided by the mm-common module. To get started, it helps a lot to pick an existing binding module as an example to look at.

For instance, let's pretend that we are wrapping a C library called libsomething. It provides a GObject-based API with types named, for instance, SomeWidget and SomeStuff.

G.1.1. Copiar el esqueleto del proyecto

Typically our wrapper library would be called libsomethingmm. We can start by copying the skeleton source tree from the mm-common module. Starting with mm-common 1.0.0 this skeleton application is built with the Meson build system.

  $ git clone https://gitlab.gnome.org/GNOME/mm-common.git
  $ cp -a mm-common/skeletonmm libsomethingmm

This provides a directory structure for the source .hg and .ccg files and the hand-written .h and .cc files, with meson.build files that can specify the various files in use, in terms of Meson variables. The directory structure usually looks like this, after we have renamed the directories appropriately:

  • libsomethingmm: The top-level directory.

    • libsomething: Contains the main include file and the pkg-config .pc file.

      • src: Contains .hg and .ccg source files.
      • libsomethingmm: Contains hand-written .h and .cc files.

As well as renaming the directories, we should rename some of the source files. For instance:

$ for f in $(find libsomethingmm -depth -name '*skeleton*'); do \
    d="${f%/*}"; b="${f##*/}"; mv "$f" "$d/${b//skeleton/libsomething}"; \
  done
A number of the skeleton files must still be filled in with project-specific content later.

Tenga en cuenta que los archivos que terminan en .in se utilizan para generar archivos con el mismo nombre pero sin el sufijo .in, mediante la sustitución de algunas variables con valores reales durante la fase de configuración.

Generated files are saved in the build tree, which is separated from the source tree when meson and ninja are used.

G.1.2. Modificar archivos de construcción

Now we edit the files to adapt them to our needs. You might prefer to use a multiple-file search-replace utility for this, such as regexxer. Note that nearly all of the files provided with the skeleton source tree contain placeholder text. Thus, the substitutions should be performed globally, and not be limited to the Meson files.

Todas las menciones de skeleton deben reemplazarse por el nombre correcto de la biblioteca de C que está envolviendo, como «something» o «libsomething». De la misma manera, todas las instancias de SKELETON deben reemplazarse por «SOMETHING» o «LIBSOMETHING», y todas las apariciones de Skeleton deben cambiarse por «Something».

De la misma manera, reemplace todas las instancias de Joe Hacker por el nombre del titular de los derechos de autor, quien probablemente sea usted. Haga lo mismo para la dirección de correo-e joe@example.com.

G.1.2.1. meson.build in the top-level directory

  • Es común cuando se enlazan módulos rastrear el número de versión de la biblioteca que se está envolviendo. Entonces, por ejemplo, si la biblioteca de C está en una versión 1.23.4, el número de versión inicial del módulo de enlace sería 1.23.0. Sin embargo, evite comenzar con un número de versión menor par, ya que generalmente indica una versión estable.
  • In the project() function, change the license and the C++ version, if necessary.
  • You probably need to add more required modules than glibmm and skeleton (libsomething).

G.1.2.2. Other meson.build files

Next we must adapt the other meson.build files:

  • skeleton/meson.build: Perhaps not much to change here more than the global name substitutions.

  • skeleton/skeletonmm/meson.build

    defs_basefiles

    If we have more .defs and docs.xml files, we add them here.

    hg_ccg_basenames

    We must mention all of our .hg and .ccg files here.

    extra_cc_files, extra_h_files

    Any additional hand-written .h and .cc source files go here.

G.1.2.3. Crear archivos .hg y .ccg

Ahora se deben crear los primeros archivos .hg y .ccg, para envolver uno de los objetos en la biblioteca de C. Ya existen un par de archivos de fuentes de ejemplo: skeleton.ccg y skeleton.hg. Cree copias de estos archivos si es necesario.

En la sección archivos .hg y .ccg puede aprender acerca de la sintaxis usada en estos archivos.