使用介子时如何指定库路径?

How can I specify library path when using Meson?

我正在尝试使用 Meson 构建一个 C++ 项目。

问题是,我在 /opt/conda 下有一些库 但无法弄清楚如何在 运行 meson build 时 link 项目。 似乎只能搜索 /usr/lib 目录。

据我了解,meson 使用 cmakepkg-config 来查找库。 那么设置 CMAKE_PREFIX_PATH 之类的内容是否可行?如果可以,我该怎么做?

提前致谢。

如果我对 documentation 的理解正确,您可以使用不同/其他构建系统作为子项目,它似乎不是基于 cmake。 您应该能够在 cmake 项目的 CMakeList.txt 中定义 CMAKE_PREFIX_PATH,并在 meson 上下文中访问生成的库: 在你的 cmake 子项目中:

add_library(cm_lib SHARED ${SOURCES})

在你的介子中:

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])

如果您只想将任何特定库传播到 meson,那么您似乎需要 bundle those third party library, or using built-in options

但首先:您是否检查过,/opt/conda是否在您的LD_LIBRARY_PATH中?

我看到了两种可能的方法来解决您的问题。

  • 第一个解决方案使用 LIBRARY_PATH,这与稍后解释的 LD_LIBRARY_PATH 不同。
  • 第二种解决方案使用修改后的介子文件直接将选项传递给链接器。可选地,它还使用 rpath 消除了之后修改 LD_LIBRARY_PATH 的需要。

    1. 第一个解

构建项目时,链接器使用 LIBRARY_PATH(而不是 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.

更多详情LD_LIBRARY_PATH vs LIBRARY_PATH

也许你可以试试

export LIBRARY_PATH=/opt/conda/:$LIBRARY_PATH

在 运行 介子构建您的项目之前。

  1. 第二种解法

修改介子文件并使用rpath(可选)

前面第一个解决方案的替代方法是直接修改您的 Meson 文件以将一些选项传递给 链接器

这是我过去用过的东西,你可以很容易地适应你的问题:

#
# 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(...) 定义传递给 linker 的选项(这替换需要在第一个方案中定义LIBRARY_PATH)

  • 可执行文件(...) 定义 rpath。这是将额外的库路径信息直接嵌入到可执行文件中的可选步骤。如果你使用它,你将不必修改 LD_LIBRARY_PATH when 运行 你的可执行文件。

更多详情: 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