Meson: how to make find_library() works with an unusual path?
Asked Answered
P

3

12

For my Meson project I have a dependency that is in an "unusual" place:

/opt/MyDependence/lib/libmyLib.so
/opt/MyDependence/include/myLib.hpp

My meson file is:

project('Test', ['cpp'])

cpp = meson.get_compiler('cpp')
myLib_dep = cpp.find_library('myLib', required: true)

Obviously Meson cannot find the library

Meson.build:5:0: ERROR: C++ library 'myLib' not found

The problem is that I do not know the "canonical" way to add extra search paths so that Meson can found my lib. Any idea?


update: please note that even if I use:

meson --libdir=/opt/MyDepedence/lib build

I get this error message:

meson.build:1:0: ERROR: The value of the 'libdir' option is '/opt/MyDepedence/lib' which must be a subdir of the prefix '/usr/local'.
Note that if you pass a relative path, it is assumed to be a subdir of prefix.
Pinkiepinkish answered 16/1, 2020 at 12:42 Comment(2)
check my update regarding --libdirMitman
@Mitman thank you for the clarificationPinkiepinkish
U
14

find_library now has an optional argument dirs (since 0.53.0) that indicates an extra list of absolute paths where to look for program names.

cpp = meson.get_compiler('cpp')
myLib_dep = cpp.find_library('myLib', dirs: '/opt/MyDepedence/lib', required: true)
Unchancy answered 1/6, 2020 at 12:58 Comment(0)
P
7

I finally got a solution, one must use LIBRARY_PATH

export LIBRARY_PATH=/opt/MyDepedence/lib
meson build

Note: attention this is not LD_LIBRARY_PATH, see there for the difference

Also read this Meson/issues/217 . For Windows, the LIBRARY_PATH equivalent seems to be LIBPATH (but I was not able to check as I only run under Linux).


An alternative is to "manually" define a new dependence. In your Meson project:

project('Test, ['cpp'])

myLib_dep = declare_dependency(link_args : ['-L/opt/MyDependence/lib', '-lmyLib'],
                               include_directories : ['/opt/MyDependence/include'])


exe1 = executable('main', ['main.cpp'], dependencies : [myLib_dep])

A refinement that could be done is to store this "manual" setting into meson_options.txt.


Note: I finally answered my question, but I am still open to better solutions.

Pinkiepinkish answered 16/1, 2020 at 15:39 Comment(1)
The “alternative” is actually a great answer, which I used this way. That’s the exact solution for the moment when you want to quickly test something. At that moment you don’t want to start building a new portal leading into the exquisite meson cathedral, but instead you need to take an ugly rusty excavator and make a hole in the cathedral’s wall to get stuff in. No matter if it’s portable, properly versioned, safe or whatever, just make it run right now. Thanks for the “alternative”; it really helps in such cases!Dowse
M
2

Standard way to solve this is to use pkg-config. In short, library installation procedure should include stage where special "mylib.pc" file is generated (from scratch, or typically from template "mylib.pc.in" - search in the internet, there are lots of examples). Then these small key-value files which has info on include/library dirs, dependencies, etc are installed to some known location, typically /usr/lib/pkgconfig/. Meson naturally runs pkg-config under the hood and finds your library when you have something like this

mylib_dep = dependency('mylib', required: true)

in your meson.build.

Update

Regarding libdir meson option error, you can try add option prefix as well:

meson --prefix=/opt/MyDepedence --libdir=lib build

Note also that with this command line you actually call implicitly setup command (there is no command build, since you will build with ninja) and build is a build directory that will be created using your options. Check this. That is why it is more visible to write:

meson setup build_dir --prefix=/opt/MyDepedence --libdir=lib
Mitman answered 17/1, 2020 at 15:27 Comment(1)
Thanks, just wanted to add about "unusual path", sometimes you'd even want to install mylib.pc to unusual location and in this case adjusting for PKG_CONFIG_PATH is needed (askubuntu.com/questions/210210/…)Mitman

© 2022 - 2024 — McMap. All rights reserved.