Splitting a project into different executable files

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Oct 12 03:37:13 UTC 2024


On Friday, October 11, 2024 7:09:56 PM MDT Alexander Zhirov via Digitalmars-d-
learn wrote:
> Is it possible to organize a project that will consist of several
> main files. I want to write several simple programs in one
> project, but for them to be divided into different files. So that
> it would be possible to choose during compilation what to build,
> via dub. Is this possible?

I can think of two ways to do this:

1. Have a single main but have it decide which program it's running by
looking at the name of the program that it's passed in (and then you can
have different functions that act like main for each program which aren't
actually called main but instead are called from main, allowing you to have
those in separate files). Then you copy your executable (or symlink it) to
different names. I've done this upon occasion. It doesn't require anything
special with regards to the build.

2. Make up a version identifier that you use for each program and version
each main with it. Then create separate build configurations in dub which
pass the chosen version identifier as part of the build. Then you have a
separate build configuration for each executable. I don't recall at the
moment how insistent dub is about main existing in app.d (I think that it
relies on it for how it does the unit test build), so you might still want a
single main that calls "main" functions from other files, but you can use
version blocks to have main decide which to call based on the version
identifier it's being built with.

Personally, I'd just go with #1 so that I didn't have to figure out how to
deal with multiple dub configurations, since that's always a pain. When I've
taken this approach, I've often just created a script that runs dub and then
symlinks the files (or an installation script which symlinks the files in my
bin directory when it copies them there). Even if you go with #2, you might
want a helper script to be able to just build them all with one command.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list