How can I specify library path when using Meson?
Asked Answered
D

3

12

I'm trying to build a c++ project with Meson.

The thing is, I have some libraries under /opt/conda but can't figure out how to link the project when running meson build. It seems to be only searching through /usr/lib directory.

As far as I understood, meson uses cmake and pkg-config to look for libraries. Then would setting something like CMAKE_PREFIX_PATH be a feasible solution? and if so, how can I do that?

Thanks in advance.

Diamante answered 6/4, 2020 at 11:27 Comment(0)
W
17

I see two possible approaches to solve your problem.

  • the first solution uses LIBRARY_PATH, which is different from LD_LIBRARY_PATH as explained later.
  • the second solution uses a modified meson file to directly pass options to the linker. Optionally, it also uses rpath that eliminates the need to modify LD_LIBRARY_PATH afterward.

    1. First solution

When building your project the linker use LIBRARY_PATH (and not LD_LIBRARY_PATH)

LIBRARY_PATH is used by gcc before compilation to search directories containing static and shared libraries that need to be linked to your program.

LD_LIBRARY_PATH is used by your program to search directories containing shared libraries after it has been successfully compiled and linked.

further details: LD_LIBRARY_PATH vs LIBRARY_PATH

Maybe you can try

export LIBRARY_PATH=/opt/conda/:$LIBRARY_PATH

before running meson to build your project.

  1. Second solution

Modifying your meson file and use rpath (optional)

An alternative to the previous first solution is to directly modify your Meson file to pass some options to the linker.

Here is something I used in the past you can easily adapt to your problem:

#
# blaspp
#
blaspp_lib = 'blaspp'
blaspp_lib_dir = '/opt/slate/lib'
blaspp_header_dir = '/opt/slate/include'

blaspp_dep = declare_dependency(
    link_args : ['-L' + blaspp_lib_dir, '-l' + blaspp_lib],
    include_directories : include_directories(blaspp_header_dir))

executable('test_blaspp',
       'test_blaspp.cpp',
       build_rpath : blaspp_lib_dir,
       install_rpath : blaspp_lib_dir,
       dependencies : [blaspp_dep])
  • declare_dependency(...) defines options to pass to the linker (this replaces the need to define LIBRARY_PATH in the first solution)

  • executable(...) defines rpath. This is an optional step that embeds the extra library path information directly into the executable. If you use this, you will not have to modify the LD_LIBRARY_PATH when running your executable.

Further details: https://amir.rachum.com/blog/2016/09/17/shared-libraries/ (have a look at the "rpath and runpath" section) and see wikipedia: https://en.wikipedia.org/wiki/Rpath

Warison answered 7/4, 2020 at 4:16 Comment(4)
Thanks! The second solution did the trick. Turns out, I had to modify the meson.build file anyway because the dependency() function that the given meson.build file was using doesn't seem to follow the LIBRARY_PATH.Diamante
Thanks for the feedback, I will try to have a look at the LIBRARY_PATH solutionWarison
The second solution worked for me as well. How do you use 'build_rpath' with several libs folders ? what needs to be change in the above solution for several libs and libs folders ?Balsamiferous
@yehudahs, thanks for the feedback. Did you try build_rpath : 'dir_1;dir_2;dir_3'. According to meson doc build_rpath is 'a string to add to target's rpath definition'. Not sure it works, that's my suggestion.Warison
D
2

If I understand the documentation correctly, you could use different / others build system as subproject, and it doesn't seem basing on cmake. You should be able to define CMAKE_PREFIX_PATH in a CMakeList.txt of a cmake project, and access the generated library within meson context: in your cmake subproject:

add_library(cm_lib SHARED ${SOURCES})

in your meson:

cmake = import('cmake')

# Configure the CMake project
sub_proj = cmake.subproject('libsimple_cmake')

# Fetch the dependency object
cm_lib = sub_proj.dependency('cm_lib')

executable(exe1, ['sources'], dependencies: [cm_lib])

if you only want to propagate any specific library to meson, than it looks like you'll need to bundle those third party library, or using built-in options.

But first of all: Have you check, either /opt/conda is in your LD_LIBRARY_PATH ?

Declass answered 6/4, 2020 at 20:6 Comment(1)
Thanks @Declass Yes I do have /opt/conda/lib in my LD_LIBRARY_PATH but it didn't really help when configuring build environment. The project I was trying to build was based on GStreamer and most part of their meson.build file was given. I just wanted to include the libraries so reconfiguring it to cmake subproject wasn't a solution I was looking for. But the bundling third party library seems feasible so I'll look into it.Diamante
A
2

Surprised no one mentioned it but this is how it is done from within meson.

CXX = meson.get_compiler('cpp')
libs_you_need_to_link = ['lib_a', 'lib_b', 'lib_c']
deps = []
foreach lib_name : libs_you_need_to_link 
    deps += CXX.find_library(lib_name, dirs : ['/opt/conda', '/other/path'])
endforeach
Almoner answered 1/8, 2022 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.