使用Meson时如何指定库路径? [英] How can I specify library path when using Meson?
问题描述
我正试图用Meson构建一个c ++项目.
I'm trying to build a c++ project with Meson.
问题是,我在/opt/conda
下有一些库但无法确定在运行介子构建
时如何链接项目.它似乎只在/usr/lib
目录中搜索.
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.
据我了解,介子
使用 cmake
和 pkg-config
查找库.那么设置类似 CMAKE_PREFIX_PATH
的方法是否可行?如果是这样,我该怎么办?
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?
谢谢.
推荐答案
我看到了两种可能的方法来解决您的问题.
I see two possible approaches to solve your problem.
- 第一个解决方案使用
LIBRARY_PATH
,这与LD_LIBRARY_PATH
不同,如稍后所述. -
第二个解决方案使用修改后的介子文件将选项直接传递给链接器.(可选)它还使用
rpath
,从而无需在以后修改LD_LIBRARY_PATH
.
- the first solution uses
LIBRARY_PATH
, which is different fromLD_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 modifyLD_LIBRARY_PATH
afterward.
- 第一个解决方案
在构建项目时,链接器使用 LIBRARY_PATH
(而不是 LD_LIBRARY_PATH
)
When building your project the linker use LIBRARY_PATH
(and not LD_LIBRARY_PATH
)
gcc在编译之前使用
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
搜索目录成功编译后包含共享库并链接.
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与LIBRARY_PATH
也许您可以尝试
export LIBRARY_PATH=/opt/conda/:$LIBRARY_PATH
在运行介子来构建项目之前.
before running meson to build your project.
- 第二个解决方案
修改介子文件并使用 rpath
(可选)
Modifying your meson file and use rpath
(optional)
先前第一个解决方案的替代方法是直接修改您的Meson文件,以将某些选项传递给链接器.
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(...)定义了传递给链接器的选项(这取代了在第一个解决方案)
declare_dependency(...) defines options to pass to the linker (this replaces the need to define
LIBRARY_PATH
in the first solution)可执行文件(...)定义
rpath
.这是一个可选步骤,可将额外的库路径信息直接嵌入到可执行文件中.如果使用此选项,则在运行可执行文件时不必修改LD_LIBRARY_PATH
.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 theLD_LIBRARY_PATH
when running your executable.更多详细信息:: https://amir.rachum.com/blog/2016/09/17/shared-libraries/(请查看"rpath和runpath"部分)并查看维基百科:
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
这篇关于使用Meson时如何指定库路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!