如何在DUB中进行配置,将具有主功能的多个输入D文件转换为多个输出可执行文件?
我至少可以想到一些这样的用例或场景:如何在DUB中进行配置,将具有主功能的多个输入D文件转换为多个输出可执行文件?,d,dub,D,Dub,我至少可以想到一些这样的用例或场景: 构建多个示例程序的“示例”配置 带有客户端程序和宿主程序的项目 包含一个主程序和一些相关实用程序(例如:gcc)的项目 更一般地说,如果项目有多个具有相同或相似依赖集的输出可执行文件,那么一次构建它们可能是有意义的。对于用户来说,决定运行哪个可执行文件要比弄清楚如何让DUB创建他们想要的可执行文件更容易(他们可能不是熟悉DUB的D开发人员)。作为一名D开发人员,这对我来说也很方便,因为我可以运行更少的构建命令来获得我想要的东西 例如,我可能有这样一个项目
- 构建多个示例程序的“示例”配置
- 带有客户端程序和宿主程序的项目
- 包含一个主程序和一些相关实用程序(例如:gcc)的项目
dub.json
.gitignore
source/common/foo.d (Code called by all examples)
source/examples/simple_example.d (Build input with main function)
source/examples/complex_example.d (Build input with main function)
source/examples/clever_example.d (Build input with main function)
bin/simple_example.exe (Output executable)
bin/complex_example.exe (Output executable)
bin/clever_example.exe (Output executable)
dub.json
.gitignore
source/common/netcode.d (Code called by all programs)
source/common/logic.d (Code called by all programs)
source/executables/host-daemon.d (Does privileged things for the server)
source/executables/server.d (Unprivileged network endpoint)
source/executables/client.d (Queries the server)
bin/host-daemon (Output executable)
bin/server (Output executable)
bin/client (Output executable)
另一个项目可能如下所示:
dub.json
.gitignore
source/common/foo.d (Code called by all examples)
source/examples/simple_example.d (Build input with main function)
source/examples/complex_example.d (Build input with main function)
source/examples/clever_example.d (Build input with main function)
bin/simple_example.exe (Output executable)
bin/complex_example.exe (Output executable)
bin/clever_example.exe (Output executable)
dub.json
.gitignore
source/common/netcode.d (Code called by all programs)
source/common/logic.d (Code called by all programs)
source/executables/host-daemon.d (Does privileged things for the server)
source/executables/server.d (Unprivileged network endpoint)
source/executables/client.d (Queries the server)
bin/host-daemon (Output executable)
bin/server (Output executable)
bin/client (Output executable)
在这两个项目中,我都希望通过一次DUB调用来构建所有可执行文件。理想情况下,由于输入和输出的相互关联性,这一切都将从一个dub.json文件进行管理
似乎可以做到这一点,但从一个dub.json文件管理它“通常不鼓励”:
子目录/component1和/component2随后包含普通包,可以从外部项目中称为“mylib:component1”和“mylib:component2”。要引用同一存储库中的子包,请使用“*”版本说明符
也可以在根包文件中定义子包,但请注意,通常不建议将多个子包的源代码放入同一源文件夹。这样做可能会导致对“依赖项”部分中未明确说明的子包的隐藏依赖项。这些隐藏的依赖关系可能会导致生成错误,并与某些生成模式或可能难以理解的依赖关系树相结合
DUB可以像上面那样一次性构建多个可执行文件吗?如果可以,最推荐的方法是什么?为什么
相反,您应该能够创建多个配置来做您想做的事情(它定义了多个库,但您也可以轻松地执行多个可执行文件)
我不确定是否有一个命令可以一次构建所有配置<代码>--可能是组合的
(文档不清楚,但我认为它实际上是用一个编译器调用来构建所有源文件,而不是一个接一个地生成目标文件)。我成功地使用了配置,没有子包
首先,用versions
说明符作为所有主要函数的前缀
假设您有3个不同的文件,每个文件都有自己的主文件
一、d:
....
versions(one)
void main() {
....
}
二、d:
....
versions(two)
void main() {
....
}
三、d:
....
versions(three)
void main() {
....
}
在dub项目文件中(我在这里使用sdl格式),可以定义三种配置,以便每个配置定义不同的版本,并输出到不同的输出文件
配音sdl:
....
configuration "one" {
versions "one"
targetType "executable"
targetName "one"
}
configuration "two" {
versions "two"
targetType "executable"
targetName "two"
}
configuration "three" {
versions "three"
targetType "executable"
targetName "three"
}
如果您只调用dub build
,它将使用第一个配置,但您可以传递不同的配置:
dub build --config=two
谢谢你链接论坛帖子。看起来我可能会成为另一个滥用子包的人:配置似乎是为了管理输出类型和可选依赖项(如dub的json文件);为多个输出批处理它们并不像我期望的那么简单。有趣的是,D论坛的帖子链接了DUB第1155期,有人回答说“[…]如果有多个示例,情况如何?”我希望不是这样,但可能这是一个未考虑的用例。值得注意的是,当我尝试使用配置来解决这个问题时(作为权宜之计),它将失败,因为在编译一个示例文件时,它试图将所有.d文件都包含在examples子目录中(并给出关于具有多个主函数的错误消息)。这是因为我在json文件的所有级别尝试了
“excludedSourceFiles”:[“/source/examples/*”]
,并在每个配置中尝试了“mainSourceFile”:“/source/examples/some_example.d”
。因此,我甚至无法测试--组合的建议:(在d代码中,代码本身需要使用版本(不是复数版本)