MesonBuild:如何定义对 `pkg-config` 找不到的库的依赖?

MesonBuild: How to define dependency to a library that cannot be found by `pkg-config`?

我的项目(在 C 中)在构建时具有第三方依赖性。但是第三方库默认安装到 /opt/ 而不是 /lib,我在 pkg-config 找不到它。从mesonbuild的文档来看,我应该使用declare_dependency,我没有它的源代码来把它当作我的子项目。如果我使用 dependency() 来定义它,我找不到正确的参数来定义自定义位置。

如何声明对非标准第三方库的依赖?

如文档所述here and here

The main use case for this [declare_dependency()] is in subprojects.

[dependency()] finds an external dependency ... with pkg-config [or] library-specific fallback detection logic ...

您可以使用 compiler 提供的 find_library() 对象和 include_directories()find_library() returns 一个与 declare_dependency() returns 相似的对象。 include_directories() returns 包含目录的不透明对象。

假设您使用的是 C 编译器并且您的第 3 方库及其头文件是 /opt/hello/libhello.so/opt/hello/hello.h,您可以这样做:

project('myproj', 'c')

cc = meson.get_compiler('c')
lib_hello = cc.find_library('hello',
               dirs : ['/opt/hello'])
inc_hello = include_directories('/opt/hello')
exec = executable('app',
                  'main.c',
                  dependencies : [lib_hello],
                  include_directories : inc_hello)

参考meson对象herecurrent_source_dir()方法returns一个字符串到当前源目录。

用于 libhello.solibhello.h 位于 <workspace>/hello目录

<workspace>/main.c
<workspace>/meson.build

<workspace>/hello/libhello.so
<workspace>/hello/libhello.h
<workspace>/hello/meson.build

<workspace>/hello/meson.build中:

lib_hello = cc.find_library('hello', dirs : meson.current_source_dir())

<workspace>/meson.build中:

project('myproj', 'c')
subdir('hello')

inc_hello = include_directories('./')
exec = executable('app',
                  'main.c',
                  dependencies : [lib_hello],
                  include_directories : inc_hello)