Menu

Dependencies

Jussi Pakkanen

The Meson build system has moved

The new home of Meson's documentation is here. The information below is preserved only for posterity. It should be considered out of date.


External dependencies

Very few applications are fully self-contained, but rather they use external libraries and frameworks to do their work. Meson makes it very easy to find and use external dependencies. Here is how one would use the Zlib compression library.

zdep = dependency('zlib')
exe = executable('zlibprog', 'prog.c', deps : zdep)

First Meson is told to find the external library zlib and error out if it is not found. Then an executable is built using the specified dependency. Note how the user does not need to manually handle compiler or linker flags or deal with any other minutiae.

If the dependency is optional, you can tell Meson not to error out if the dependency is not found and then do further configuration.

opt_dep = dependency('somedep', required : false)
if opt_dep.found()
  # Do something.
else
  # Do something else.
endif

You can pass the opt_dep variable to target construction functions whether the actual dependency was found or not. Meson will ignore non-found dependencies.

The dependency detector works with all libraries that provide a pkg-config file. Unfortunately several packages don't provide pkg-config files. Meson has autodetection support for some of these.

Boost

Boost is not a single dependency but rather a group of different libraries. To use Boost with Meson, simply list which Boost modules you would like to use.

boost_dep = dependency('boost', modules : ['thread', 'utility'])
exe = executable('myprog', 'file.cc', deps : boost_dep)

You can call dependency multiple times with different modules and use those to link against your targets.

GTest and GMock

GTest and GMock come as sources that must be compiled as part of your project. With Meson you don't have to care about the details, just pass gtest or gmock to find_dep and it will do everything for you. If you want to use GMock, it is recommended to use GTest as well, as getting it to work standalone is tricky.

Qt5

Meson has native Qt5 support. Its usage is best demonstrated with an example.

qt5widgets = dependency('qt5', modules : 'Widgets')

q5exe = executable('qt5test',
 sources     : ['main.cpp', 'mainWindow.cpp'],
 moc_headers : 'mainWindow.h',
 moc_sources : 'helperFile.cpp', # must have #include"moc_helperFile.cpp"
 ui_files    : 'mainWindow.ui',
 qresources  : 'resources.qrc',
 deps        : qt5widgets)

Here we have an UI file created with Qt Designer and one source and header file each that require preprocessing with the moc tool. We also define a resource file to be compiled with rcc. We just have to tell Meson which files are which and it will take care of invoking all the necessary tools in the correct order. The modules keyword of find_dep works just like it does with Boost. It tells which subparts of Qt the program uses.

At times you may need to tell Meson not to automatically build source files generated by moc because they are included manually. To do this, just specify the list of files to skip in the keyword argument manual_moc_include:

q5exe = executable('q5', sources : 'prog.cpp',
moc_headers : ['foo.h', 'bar.h'],
manual_moc_include : ['foo.h'] # prog.cpp has #include"moc_foo.cpp"
)

There is no built-in support for Qt 4 at this time.


Back to index.


Related

Wiki: Manual