我正在尝试将现有代码树移植到 centos 7 机器上的介子构建系统。介子配置工作正常,但是当我尝试编译时,它失败了。该代码是专有的,因此我创建了一个示例来说明问题(我希望足够准确。)我不能随意重构目录树。
这是树:
mesonex/
alpha/
beta/
alpha/
inc/
funcs.h
numbers.h
src/
numbers.cpp
funcs.cpp
src/
example.cpp
meson.build
我的介子.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 认为 meson.build 文件带有 project()
调用成为源目录结构的“根”。您不能要求它包含根目录之外的文件。大概是 cp /../foo .
在类 Unix 操作系统上。这可能只是您的示例中的一个错误,因为这当然不是真正的代码。
因此,如果我们将其重写为 (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
携带链接和包含信息。
关于c++ - 介子创建不会编译的构建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68355586/