Splitting a project into different executable files
David Menk
saadbaig.branex at gmail.com
Thu Jan 16 12:42:05 UTC 2025
On Saturday, 12 October 2024 at 05:22:57 UTC, evilrat wrote:
> On Saturday, 12 October 2024 at 01:09:56 UTC, Alexander Zhirov
> 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?
>
> Not sure if this what you need, but anyway.
>
> I using this project structure, have a root dub folder, and
> then have a subfolders for isolated components of the project.
> My project is game so in the future it might to have main
> executable and dedicated server, this should be able to choose
> what to build with configurations.
>
> overall here is the dub config, I have root package to build
> everything I need at once (using dependencies declaration), and
> then it should be possible to add second configuration.
>
> And as usual, dub can build any subpackage like this `dub build
> game:somepackage`.
>
> *(don't mind that it is a dynamic library, i have external app
> loading lib but principle will be the same for regular
> executables with main function)*
>
> __dub.json__
> ```json
> {
> "authors": [
> "John Smith"
> ],
> "copyright": "Copyright © 2023, John Smith",
> "description": "some game",
> "license": "proprietary",
> "name": "game",
>
> "subPackages": [
> {
> "name": "core",
> "targetType": "dynamicLibrary",
> "targetPath": "coregame/bin",
> "sourcePaths": ["coregame/source"],
> "importPaths": ["coregame/source"],
> "dependencies": {
> "gameengine" : "~>0.1.0"
> }
> }
> ],
> "dependencies": {
> "game:core": "*"
> },
> "configurations": [
> {
> "name": "default",
> "targetType": "none"
> }
> ]
> }
> ```
>
> If later I need to add dedicated server build it would be
> another entry in "configurations" something like this, where D
> version will be choosing between two main() functions or just
> modify control flow if needed.
>
> then choosing between targets will be just like this
>
> **`dub build`** for regular app
> **`dub build -c dedicated`** for server build
>
> ```json
> "configurations": [
> {
> "name": "default",
> "targetType": "none"
> },
> {
> "name": "dedicated",
> "targetType": "none",
> "versions": [ "DedicatedServer" ]
> },
> ]
> ```
Sounds like you've got a solid structure going—almost like
planning a heist, but for code! Your approach with configurations
makes it super flexible, and "dub build -c dedicated" feels like
flipping a switch for stealth mode. If you ever need real-world
hardware to run that dedicated server, check out HostNoc for
[discount dedicated servers](https://hostnoc.com/). They’d keep
your operation smooth without breaking the bank!
More information about the Digitalmars-d-learn
mailing list