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?
Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 12 Splitting a project into different executable files | ||||
---|---|---|---|---|
| ||||
October 11 Re: Splitting a project into different executable files | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexander Zhirov | 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
|
October 12 Re: Splitting a project into different executable files | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexander Zhirov | 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. 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 (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
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
|
October 12 Re: Splitting a project into different executable files | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexander Zhirov | 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? Avoid dub for this, spend the 50 lines of code to write a build script |
October 12 Re: Splitting a project into different executable files | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexander Zhirov | 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? Thank you very much everyone for the advice! I will take all the advice into account when designing. |
October 24 Re: Splitting a project into different executable files | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexander Zhirov | 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? I have a dub project that is a set of examples for my library. Every example is a separate program with its own main function: https://github.com/andrey-zherikov/argparse/blob/master/examples/dub.json |