Writing a Library with Vala:

Naming Conventions and Building the Library

Building the Binary

Unix Shared Object File

Windows DLL File

Generating Interface Files

C Header

The --header option passed to valac generates a C header for the library:

valac \
  --hide-internal \
  -X -shared \
  -X -fpic \
  --library mylibrary \
  --header mylibrary.h \
  mylibrary_source.vala

This will generate a C header file named mylibrary.h. The naming conventions for C header files should be followed.

The C header file is also used by Vala based bindings and GObject Introspection based bindings so it will always need to be generated. Even if you do not intend your library to be used directly from C code.

Vala VAPI

The --vapi option passed to valac generates a Vala VAPI file for the library:

valac \
  --hide-internal \
  -X -shared \
  -X -fpic \
  --library mylibrary \
  --header mylibrary.h \
  --vapi mylibrary.vapi \
  mylibrary_source.vala

This will generate a VAPI file named mylibrary.vapi. The name of the VAPI file should be the same as the pkg-config file for the library.

A VAPI will always be generated even when the --vapi option is not used. When --vapi is not given valac will append .vapi to the library name given with the --library option. It is better to be explicit and use the --vapi option.

GObject Introspection's GIR and Typelib Files

The --gir option passed to valac generates a GObject Introspection Repository (GIR) file for the library:

valac \
  --hide-internal \
  -X -shared \
  -X -fpic \
  --library mylibrary \
  --header mylibrary.h \
  --vapi mylibrary.vapi \
  --gir MyLibrary-1.0.gir \
  mylibrary_source.vala

This will generate a GIR file named MyLibrary-1.0.gir. The name of the GIR file should follow the GObject Introspection naming conventions and include an API version number.

A typelib file can then be generated from the GIR using g-ir-compiler:

g-ir-compiler --output MyLibrary-1.0.typelib MyLibrary-1.0.gir

GIR files are typically used to generate compile time bindings. Typelib files are used to create runtime bindings and a binding generator will read them using libgirepository.

Example Projects and their Build Tools

Some example projects and tutorials:

  • vala_library_tutorial - "Simple tutorial on how to start with making your own library in Vala", uses a simple Makefile that clearly shows the stages and commands involved in producing the binary, GIR file and typelib file

  • Creating a Shared Library in Vala - code sample and tutorial from the GNOME Wiki

  • vala-object - "Use Vala from Ruby, Python, Lua, JavaScript (Node.js, gjs, seed) and many other languages", small example project that also shows the library being used by other languages

  • gherkin-vala - "Port of the Gherkin language to Vala", uses Autotools to build the binary

Projects/Vala/LibraryWritingNamingAndBuilding (last edited 2018-07-15 22:36:59 by AlThomas)