Writing Program Without main Function

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Dec 7 21:26:48 UTC 2018


On Friday, December 7, 2018 2:02:59 PM MST Samir via Digitalmars-d-learn 
wrote:
> Is it possible to write and execute a D program without a main
> function?
>
> Most of my programs will start with some `import` statements,
> followed by any functions and then ending with the `main`
> function (e.g. `void main() {`).
>
> As I am just a beginner to programming and still new to D, I have
> been browsing some repositories that people have posted here to
> learn more about the language and how more experienced
> programmers write their code.  In many of these examples, they
> start with the `module` statement followed by their `import`
> statements and then will have sections defining various classes,
> structs, unit tests and other bits but I never see a `main`
> function.
>
> How do you compile and execute these programs?

The short answer is that every program must have a main function.

The long answer:

main is the entry point into the program. You can't do much without it, and
the linker will require that it exists. static constructors do get run
before main, and static destructors get run after it, so it is possible to
run code before and after main, but they're only intended for more complex
initialization and destruction of module-level and static variables. Really,
when it comes down to it, main _is_ your program, and there must be a main
function one way or another. That's true of pretty much any programming
language. Some may present main in a different way, but ultimately, every
programming language has some form of main, because every program needs an
entry point.

dmd provides the -main flag which will insert an empty main, so you could
use that, but it's really only of any use for unit testing (since in D,
unittest blocks are run before main). For an actual program, you basically
don't have anything without main. Essentially what you get in D is

1. Run static constructors.
2. If -unittest was used, run the unittest blocks (though since that's only
   intended for testing, it really shouldn't be in a production program).
3. Run main.
4. Run static destructors.

And for simpler programs, you basically have

1. Run main.

None of your stray functions or classes are going to be used just because
you declared them. It's main that decides what's going to be run, even if
it's just to call another function. All of those other functions and classes
are just a way to organize your program, make pieces reusable, etc.
Ultimately, the main function is your program, so it really doesn't make
sense to try to not have one.

The closest thing that exists to a program with no main is a library, and
libraries are just collections of functions and user-defined types which are
packaged together so that programs can use them. They aren't programs on
their own, just pieces of reusable code. Ultimately, you need an actual
program with a main to use them.

When you see code snippets that don't use main, it's because they're usually
just that - code snippets. They're showing a piece of code, not an entire
program. It's just assumed that anyone who wants to actually run them is
going to deal with declaring main themselves, because everyone knows that
you need a main function. As such, including main is often just extraneous
information.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list