I found the trick to make dependency on another package:

dub add-local {root-project}

add dependency using:

dependency {root-project:subpackage} version="*"

But I can not get the first scenario--when the subpackage denpend on the root package--work,
as dub will complaint that it finds dependency cycles and fail.

In fact, all I want to do is just adding some additional executables to my project, what I want is:

  • A single repo
  • All util or common libs together or in differenct subpackages
  • Define multiple executables that may need the shared util/common libs

Can we use additional target instead of subpackages for additional executables? What I want:

name "my-project"
....


dependency ":net-util" version="*" # a subpackage lib

subpackage "./net-util/" 

targetName "my-app"
targetType "executable"

target "my-other-app" {
  targetType "executable"
  dependency ":util" version="*"
}

target "my-third-app" {
  targetType "executable"
  dependency ":net-util" version="*"
}

Where my-other-app and my-third-app needs functions defined in the root package, so making them a subpackage will lead to the cycle-dependency problem I described earlier. So they better belong to the root package.

Then you get three executables in the root directory: my-app, my-other-app and my-third-app.

Is that possible?

IMO subpackages are better used as util/libraries instead of additional executables.

In cmake if you want more executables, just add one with add-executable in the root package. It is very intuitive.

On Fri, 19 Feb 2016 03:23:39 GMT, zhaopuming wrote:

Another question: How can the subPackage code use modules defined in the root package? or defined in another subPackage?

On Wed, 09 Sep 2015 07:10:31 GMT, Sönke Ludwig wrote:

On Tue, 08 Sep 2015 06:43:37 GMT, zhaopuming wrote:

How do I specify in dub.json that I want multiple targets?

I always get "only one main allowed" error.

The idea is to have a "targetType": "none" root package:

{
  "name": "myproject",
  "targetType": "none",
  "subPackages": [
    {
      "name": "target1",
      "targetType": "executable",
      ...
    },
    {
      "name": "target2",
      "targetType": "executable",
      ...
    }
  ]
}

The individual targets can then be built with dub build :target1 and so on. Planned is that just invoking dub build would build all targets and that dub generate visuald would for example build a solution file that contains all targets, but this is still open: #97