DUB:使用公共代码库创建两个可执行文件

时间:2016-02-27 17:04:25

标签: d dub

我需要生成两个具有一些常见源代码的exe文件。用dub做这件事的最佳方法是什么?

我尝试像this那样做,但收到的错误消息只允许一个主要功能。

这是我的dub.json:

{
    "name": "code1",
    "authors": [ "Suliman" ],
    "description": "A minimal D application.",
    "copyright": "Copyright © 2016, Suliman",
    "license": "proprietary",
    "subPackages": [
    {
        "name": "App1",
        "targetName": "App1",
        "description": "App1",
        "targetType": "executable",
        "excludedSourceFiles" : ["source/App2/*"],
        "excludedSourceFiles" : ["source/app2.d"]
    },

    {
        "name": "App2",
        "targetName": "App2",
        "description": "App2",
        "targetType": "executable",
        "excludedSourceFiles" : ["source/App1/*"],
        "excludedSourceFiles" : ["source/app1.d"]
    }]
} 

1 个答案:

答案 0 :(得分:6)

您的dub.json会有效,但您需要明确告诉它构建其中一个 包含dub build :App1dub build :App2的子包(其中:App1为a code1:App1)的快捷方式。

单独的配置可能更合适:

"configurations": [
    {
        "name": "App1",
        "targetType": "executable",
        "mainSourceFile": "source/app1.d",
        "excludedSourceFiles": [ "source/app2.d", "source/App2/*" ],
        "targetName": "app1"
    },
    {
        "name": "App2",
        "targetType": "executable",
        "mainSourceFile": "source/app2.d",
        "excludedSourceFiles": [ "source/app1.d", "source/App1/*" ],
        "targetName": "app2"
    }
]

dub build --config=App1将生成app1dub build --config=App2将生成app2

普通dub build默认为App1

请注意,您需要excludedSourceFiles,因此dub看不到重复的main

The docs建议反对 为此目的使用子包:

  

也可以在根包文件中定义子包,但请注意,通常不鼓励将多个子包的源代码放在同一个源文件夹中。这样做会导致对“依赖项”部分中未明确声明的子包的隐藏依赖性。这些隐藏的依赖项可能会导致构建错误以及某些可能难以理解的构建模式或依赖树。

我意识到你正在使用dub.json,所以我把json格式放在上面。对于 参考,这是我之前发布的dub.sdl格式。

configuration "App1" {
    targetType "executable"
    mainSourceFile "source/app1.d"
    excludedSourceFiles "source/app2.d" "source/App2/*"
    targetName "app1"
}

configuration "App2" {
    targetType "executable"
    mainSourceFile "source/app2.d"
    excludedSourceFiles "source/app1.d" "source/App1/*"
    targetName "app2"
}