4

I want to use Meson to build a new c++ project. The first thing I need is a dependency for the Boost library. But though the Boost libs are installed on my Arch system (headers and libs), Meson complains that it doesn't find them.

Here is the meson build file:

project('myproj', 'cpp')
boost_dep = dependency('boost')
executable('myproj', 'main.cpp', dependencies : boost_dep)

The main.cpp source file:

int main()
{
    return 0;
}

A partial listing of some Boost files installed on my system:

$ ls /usr/lib/libboost*|head -n5; ls /usr/include/boost/*|head -n5
/usr/lib/libboost_atomic.a
/usr/lib/libboost_atomic.so
/usr/lib/libboost_atomic.so.1.65.1
/usr/lib/libboost_chrono.a
/usr/lib/libboost_chrono.so
/usr/include/boost/aligned_storage.hpp
/usr/include/boost/align.hpp
/usr/include/boost/any.hpp
/usr/include/boost/array.hpp
/usr/include/boost/asio.hpp

Output from ninja command inside my project:

[0/1] Regenerating build files.
The Meson build system
Version: 0.43.0
Source dir: /home/io/prog/myproj/src
Build dir: /home/io/prog/myproj/builddir
Build type: native build
Project name: myproj
Native C++ compiler: c++ (gcc 7.2.0)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Dependency Boost () found: NO

Meson encountered an error in file meson.build, line 2, column 0:
Dependency "boost" not found

[...]

What am I missing?

yolenoyer
  • 469
  • 3
  • 20

2 Answers2

2

The following issue solved my problem:

Boost not detected on Fedora · Issue #2547

I replaced the meson build file by the following:

project('myproj', 'cpp')
cxx = meson.get_compiler('cpp')
boost_dep = [
    cxx.find_library('boost_system'),
    cxx.find_library('boost_filesystem'),
]
executable('myproj', 'main.cpp', dependencies : boost_dep)
yolenoyer
  • 469
  • 3
  • 20
2

The problem was fixed with Fix detection of include dirs with gnu compiler and non US locale and to find and use dependencies in meson you should use dependency().

To find boost in general you should have this in your meson.build:

project('myproj', 'cpp')
deps = [
    dependency('boost')
]
executable('myproj', 'main.cpp', dependencies: deps)

or this if you want specific parts of boost:

project('myproj', 'cpp')
deps = [
    dependency('boost', modules: ['system', 'filesystem'])
]
executable('myproj', 'main.cpp', dependencies: deps)

If you use a combination of cxx = meson.get_compiler('cpp') and cxx.find_library('boost_system'), you will not get the compiler and/or linkers flags. find_library() is primitive compiler check and only tests for shared libraries in /usr/lib. It's up to the user to make sure the headers are available with has_header() and manually define include directory with declare_dependency(include_directories: '/usr/local/include/xxx)`.

dependency() is the better way to find things and you should only use find_library() if the project doesn't support pkg-config or cmake.

ByteNudger
  • 717
  • 7
  • 20
  • 1
    This should be the coined answer of OP's question. This is the correct way to define a dependency to boost, as you cannot declare your own dependency which uses external libraries. Thanks, ByteNudger – kevr Jan 23 '22 at 23:24
  • only works when i remove the `modules` parameter, but then i get link errors – milahu Mar 02 '22 at 21:40