Installed .so library not found at runtime

1,888 views
Skip to first unread message

hjuvi hjuvi

unread,
Nov 4, 2016, 5:39:46 PM11/4/16
to The Meson Build System
Hi,

I'm currently converting a project from CMake to Meson.
It creates two binaries and a .so library, used by the binaries.

With CMake, the library was installed in /usr/local/lib.
In the installed binaries, the RPATH was set to /usr/local/lib, so that the library could be found at runtime.
(this can be checked with: readelf -a /usr/local/bin/mybinary | grep RPATH)

With Meson, the library is installed in /usr/local/lib64 (due to 64bit arch), which is fine.
In the installed binaries, there is no RPATH. And it fails at finding the library at runtime.

The definition looks like this:

mylib = shared_library('mylib', mylib.c, install : true)
executable('mybinary', mybinary.c, link_with : mylib, install : true)

The RPATH would not be necessary if the library was installed in /usr/lib for instance. But the default installation prefix is /usr/local (which is fine, and the same as CMake).

My use case is so "simple" that I'm surprised it doesn't work... I may have forgotten something.
Any idea?

NB: I'm working on an up-to-date version of Fedora.
Meson version is 0.35.0.

Regards.

Jussi Pakkanen

unread,
Nov 14, 2016, 12:20:56 PM11/14/16
to hjuvi hjuvi, The Meson Build System
On Fri, Nov 4, 2016 at 11:39 PM, hjuvi hjuvi <goo...@hjuvi.lautre.net> wrote:

> With CMake, the library was installed in /usr/local/lib.
> In the installed binaries, the RPATH was set to /usr/local/lib, so that the
> library could be found at runtime.
> (this can be checked with: readelf -a /usr/local/bin/mybinary | grep RPATH)

This is not really the recommended way to behave. Rpath should not be
set on exes installed to common dirs. The suggested way to make this
work is to set LD_LIBRARY_PATH=/usr/local/lib so it is found.

Meson does permit you to set the value that rpath should take upon
install. In your case you would do something like this:

executable(..., install_rpath : '/usr/local/lib')

Tanu Kaskinen

unread,
Nov 14, 2016, 2:09:37 PM11/14/16
to meson...@googlegroups.com
On Mon, 2016-11-14 at 19:20 +0200, Jussi Pakkanen wrote:
> On Fri, Nov 4, 2016 at 11:39 PM, hjuvi hjuvi <goo...@hjuvi.lautre.net> wrote:
>
> > With CMake, the library was installed in /usr/local/lib.
> > In the installed binaries, the RPATH was set to /usr/local/lib, so that the
> > library could be found at runtime.
> > (this can be checked with: readelf -a /usr/local/bin/mybinary | grep RPATH)
>
> This is not really the recommended way to behave. Rpath should not be
> set on exes installed to common dirs. The suggested way to make this
> work is to set LD_LIBRARY_PATH=/usr/local/lib so it is found.

Another solution (which I find nicer than messing with environment
variables) is to put /usr/local/lib64 to /etc/ld.so.conf or to a file
under /etc/ld.so.conf.d/. Debian does this by default for
/usr/local/lib, I don't know why Fedora doesn't.

--
Tanu

https://www.patreon.com/tanuk

hjuvi

unread,
Nov 15, 2016, 5:11:51 AM11/15/16
to Jussi Pakkanen, ta...@iki.fi, The Meson Build System
Hi,

Thank you for your answers.

Le 2016-11-14 18:20, Jussi Pakkanen a écrit :
> This is not really the recommended way to behave. Rpath should not be
> set on exes installed to common dirs. The suggested way to make this
> work is to set LD_LIBRARY_PATH=/usr/local/lib so it is found.

Le 2016-11-14 20:09, Tanu Kaskinen a écrit :
> Another solution (which I find nicer than messing with environment
> variables) is to put /usr/local/lib64 to /etc/ld.so.conf or to a file
> under /etc/ld.so.conf.d/. Debian does this by default for
> /usr/local/lib, I don't know why Fedora doesn't.

In fact, I'm aware of LD_LIBRARY_PATH and /etc/ld.so.conf, and it is not
a problem for me to execute the program on my computer.

My problem is that I want to provide a source-code package that people
can easily install and execute on their computer, or even package for a
distro, with "--prefix /usr".

People should only call:
$ mkdir build
$ meson build
$ ninja -C build
# ninja -C build install

That's why I don't want to hard-code "install_rpath : '/usr/local/lib'"
in meson build files.
Moreover, I cannot hard-code this value : on my computer, it is not
/usr/local/lib, it is /usr/local/lib64.

Maybe the problem is specific to Fedora. /usr/local/bin is in the PATH,
it is strange that /usr/local/lib and /usr/local/lib64 are not searched
for.
I don't have the opportunity to test on Debian for the moment.

I agree that RPATH should not be set, provided that libraries are
installed into standard directories. The problem is that Meson decides
that /usr/local/lib64 is standard directory - which I agree with - but
at run-time it is not considered as a standard directory.

I don't want to ask people to use "--prefix /usr", because we should not
mix packages provided by the distro, and packages compiled by a user
(/usr/local is fine for that).

This is the last thing I have to fix to switch from CMake to Meson :)

I want to use things in the "standard way", and I expect them to work
"out of the box" :)

Should I just warn people, in my README, that they may need to add
/usr/local/lib or /usr/local/lib64 in /etc/ld.so.conf.d/myprog?

It does not sound very nice, but it may be the only proper solution...

Regards,
hjuvi

Noam Meltzer

unread,
Nov 16, 2016, 12:13:40 AM11/16/16
to hjuvi, Jussi Pakkanen, ta...@iki.fi, The Meson Build System
You can try using -Wl,-rpath,'$ORIGIN/../lib'
Another thought would be to provide a spec file for building an RPM. Feodra has nice macros for using within RPM spec file.

hjuvi

unread,
Nov 16, 2016, 11:28:21 AM11/16/16
to Noam Meltzer, Jussi Pakkanen, ta...@iki.fi, The Meson Build System
Hi,

Le 2016-11-16 06:13, Noam Meltzer a écrit :
> You can try using -Wl,-rpath,'$ORIGIN/../lib'

It should work, but once again I have to hard-code the path (lib), maybe
hard-code two different possible paths (lib and lib64) (because Meson
does not provide the path where .so are installed), and moreover it
becomes useless if the installation is done with --prefix /usr, because
this is a standard path.

> Another thought would be to provide a spec file for building an RPM.
> Feodra has nice macros for using within RPM spec file.

This could be an evolution, and I may have a look at that later, but for
the moment I'm only trying to provide a package that anyone can compile
locally on his computer, whatever the distribution is.

Well, anyway, I'm satisfied with the answers that have been provided,
and the question can be closed for me. I agree that RPATH should not
need to be set, and - although I have not tested on Debian - I think
that the problem is the way Fedora considers standard paths.

My solution is that I will mention the possible problem in the INSTALL
file:

With some GNU/Linux distributions such as Fedora, the shared library
installed in /usr/local/lib (or lib64) might not be found at run-time.
In that case, you'll need to specify the path:

# echo "/usr/local/lib64" > /etc/ld.so.conf.d/myprog.conf
# ldconfig

(ldconfig updates ld.so.cache)

And that's it :)

Thank you all for your help.

Regards,
hjuvi

Noam Meltzer

unread,
Nov 16, 2016, 12:04:52 PM11/16/16
to hjuvi, Jussi Pakkanen, ta...@iki.fi, The Meson Build System
On Wed, Nov 16, 2016 at 6:28 PM hjuvi <goo...@hjuvi.lautre.net> wrote:
Hi,

Le 2016-11-16 06:13, Noam Meltzer a écrit :
> You can try using -Wl,-rpath,'$ORIGIN/../lib'

It should work, but once again I have to hard-code the path (lib), maybe
hard-code two different possible paths (lib and lib64) (because Meson
does not provide the path where .so are installed), and moreover it
becomes useless if the installation is done with --prefix /usr, because
this is a standard path.
afaik, meson is installing to <prefix>/lib so the above "hardcoded" should work just fine.
otoh, if meson is installing to <prefix>/lib64 you can also try:
-Wl,-rpath,'$ORIGIN/../$LIB'

anyway, that's mostly "academically speaking". it sounds like you've found your solution.

> Another thought would be to provide a spec file for building an RPM.
> Feodra has nice macros for using within RPM spec file.

This could be an evolution, and I may have a look at that later, but for
the moment I'm only trying to provide a package that anyone can compile
locally on his computer, whatever the distribution is.

Well, anyway, I'm satisfied with the answers that have been provided,
and the question can be closed for me. I agree that RPATH should not
need to be set, and - although I have not tested on Debian - I think
that the problem is the way Fedora considers standard paths.

My solution is that I will mention the possible problem in the INSTALL
file:

With some GNU/Linux distributions such as Fedora, the shared library
installed in /usr/local/lib (or lib64) might not be found at run-time.
In that case, you'll need to specify the path:

# echo "/usr/local/lib64" > /etc/ld.so.conf.d/myprog.conf
# ldconfig

(ldconfig updates ld.so.cache)

And that's it :)

Thank you all for your help.

Regards,
hjuvi

--
You received this message because you are subscribed to the Google Groups "The Meson Build System" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mesonbuild+...@googlegroups.com.
To post to this group, send an email to meson...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mesonbuild/a1cc6b129daedeb7c2bbedf35f46b89f%40hjuvi.lautre.net.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages