介子创建不会编译的构建文件

Meson creates build files which will not compile

我正在尝试将现有代码树移植到 centos 7 机器上的介子构建系统。 Meson 配置工作正常,但当我尝试编译时,它失败了。该代码是专有的,因此我创建了一个示例来说明问题(我希望足够准确。)我不能随意重组目录树。

这是树:

mesonex/
    alpha/
        beta/
            alpha/
                inc/
                    funcs.h
                    numbers.h
                src/
                    numbers.cpp
                    funcs.cpp
        src/
            example.cpp
            meson.build

我的meson.build:

project('example', 'cpp')

srcs=['example.cpp']

srcs+='../beta/alpha/src/funcs.cpp'

srcs+='../beta/alpha/src/funcs.cpp'

incdirs=include_directories('../beta/alpha/inc')

executable('example', srcs, include_directories: incdirs)

这是主要的 example.cpp 文件:

#include <iostream>
#include "../beta/alpha/inc/numbers.h"
#include "../beta/alpha/inc/funcs.h"

int main()
{
    std::cout << "Hello" << std::endl;
    std::cout << interestingNumber() << std::endl;
    std::cout << interestingFunc() << std::endl;
}

这些是支持的 cpp 文件:

// funcs.cpp
#include "../inc/numbers.h"

float interestingFunc()
{
    return (interestingNumber()+1)/2;
}

// numbers.cpp

float interestingNumber()
{
    return 11.3355;
}

这些是头文件:

// funcs.h

float interestingFunc();

// numbers.h

float interestingNumber();

请注意,目录名称中的重复是故意的。也许这让介子在弄清楚如何处理#includes 时感到困惑?

这只是我尝试过的许多不同构建策略中的一个示例。

我马上看到一个问题,它可能只是您的示例的问题,而不是您的实际代码的问题:Meson 认为带有 project() 调用的 meson.build 文件是源目录结构的“根”。您不能要求它包含根目录之外的文件。在类 Unix OS 上,它大约类似于 cp /../foo .。这可能只是您示例中的一个错误,因为这当然不是真正的代码。

所以,如果我们将其重写为 (mesonex/alpha/meson.build):

# no project(), that would be in mesonex/meson.build)

sources = files(
  'example.cpp',
  '../beta/alpha/src/funcs.cpp',
  '../beta/alpha/src/numbers.cpp',  # you have a typo in your example, funcs.cpp is listed twice.
)

executable(
  'example',
  sources,
  include_directories : include_directories('../beta/alpha/inc'),
)

应该可以。

请注意,您可能想考虑使用方便的静态库而不是返回代码,因为这是最佳实践,您可以编写类似 (mesonex/alpha/beta/meson.build):

lib_beta = static_library(
  'beta',
  ['src/funcs.cpp', 'src/numbers.cpp']
)

idep_beta = declare_dependency(
  link_with : lib_beta,
  include_directories : include_directories('.'),
)

然后在 (src/meson.build):

executable(
  'example',
  'source.cpp',
  dependencies : idep_beta
)

就是您所需要的,因为 idep_beta 包含链接和包含信息。

这是后续 - 该解决方案适用于我的示例,但不适用于实际代码。我的模型一定是不完整的(在某种程度上我还没有确定。)配置阶段有效,但在编译阶段,cpp 源代码中的#includes 被标记为“文件或目录不存在”。介子如何协调指定的包含目录与源代码中的#include 语句? #include 路径可能与实际 cpp 源的实际目录相关。这是否意味着我必须编辑所有来源中的所有#includes - 这将是一个真正的负面影响。我们使用一些非常大的代码库。