3

I need to generate two exe files that have some common source codes. What is the best way to do it with dub?

I tried to do like this, but got error message about only one main function allowed.

Here is my 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"]
    }]
} 
4

1 回答 1

7

Your dub.json will work, but you need to explicitly tell it to build one of the subpackages with dub build :App1 or dub build :App2 (where :App1 is a shortcut for code1:App1).

Separate configurations may be more appropriate here:

"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 will produce app1, dub build --config=App2 will produce app2

A plain dub build will default to App1.

Note that you need excludedSourceFiles so dub doesn't see a duplicate main.

The docs recommend against using subpackages for this purpose:

It is also possible to define the sub packages within the root package file, but note that it is generally discouraged to put the source code of multiple sub packages into the same source folder. Doing so can lead to hidden dependencies to sub packages that haven't been explicitly stated in the "dependencies" section. These hidden dependencies can then result in build errors in conjunction with certain build modes or dependency trees that may be hard to understand.

I realized you were using dub.json, so I put the json format above. For reference, here's the dub.sdl format I posted earlier.

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"
}
于 2016-02-27T17:24:38.687 回答