【问题标题】:Releasing a meson package, how to specify libraries the depending code should link with?发布介子包,如何指定依赖代码应该链接的库?
【发布时间】:2022-06-13 20:23:29
【问题描述】:

我有一个使用介子作为构建系统的项目 A 和另一个依赖于 A 的项目 B。

A 运行、编译并通过所有测试。我通过 meson install 安装了 A,它将所有标题和共享库对象放在我需要它们的位置。

安装A后我想编译B所以我添加了:

A               = dependency('A', include_type : 'system')
exe = executable(
    'B', 
    src, 
    dependencies: [
        A
    ],
    cpp_args : '-DSHADER_PATH="' +  meson.current_source_dir() + '/"',)

到B的meson.build,介子确实找到了A作为一个包并开始编译B但无法链接。 A 定义了大量的小型实用程序,每个都是自己独立的.so 二进制文件,所有这些都需要链接。查看编译 B 时执行的命令,将 A 的 .so 库所在的目录添加到使用 -L 的路径中,但该目录中没有列出用于链接的库。所以链接 fials 是因为找不到这些二进制文件中的符号(显然它们没有链接)。

当项目用作依赖项时,我需要在 A 中指定什么以使其知道默认情况下需要链接给定库?

例如,A 的一个实用程序如下所示:

renderer_so_relative_path = \
    '' + renderer_lib.full_path().replace(meson.build_root() + '/', '')
peripheral_so_relative_path = \
    '' + peripheral_lib.full_path().replace(meson.build_root() + '/', '')

loader_sources = [
    'ModuleStorage.cpp',
    'CLI.cpp'
]
install_subdir('.', install_dir : 'include/ModuleStorage/')
loader_lib = library(
    'ne_loader',
    sources : loader_sources,
    cpp_args : [
        '-DNE_RENDERER_PATH="' + renderer_so_relative_path + '"',
        '-DNE_PERIPHERAL_PATH="' + peripheral_so_relative_path + '"'
        ],
    link_with : [],
    include_directories : [],
    dependencies : [core_dep, image_dep, argparse_dep, glfw],
    install: true)

module_storage_dep = declare_dependency(link_with:loader_lib, include_directories: ['..'])

subdir('Imgui')

【问题讨论】:

    标签: c++ build-system meson-build


    【解决方案1】:

    也许这会有所帮助:

    添加到您的 A 项目中:

    # C compiler
    ccompiler = meson.get_compiler('c')
    # Where is the lib: 
    a_lib_dir = '/install/dir'
    # Find the lib: 
    a_lib = ccompiler.find_library('a_lib_name', dirs: a_lib_dir)
    # Create dependency
    a_lib_dep = declare_dependency(
      dependencies: a_lib,
      include_directories: include_directories(inc_dir)
    )
    

    并链接B中的依赖:

    # Dependencies
    b_deps = []
    b_deps += dependency('alib', fallback:['alib', 'a_lib_dep'])
    # Your B object
    b_lib = static_library( 'blib', src,
        dependencies: b_deps,
        etc...)
    # Your B object as someone's dependency (if needed):
    b_as_dep = declare_dependency(
        link_with: b_lib,
        include_directories: inc_dirs,
        dependencies: b_deps)
    

    【讨论】: