www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - undefined reference to "main"

reply Alexander Zhirov <azhirov1991 gmail.com> writes:
How to compile the example given in the book correctly? When 
compiling, an error occurs that the main function is missing. If 
I replace `shared static this()` with `void main()', then 
everything starts. What does the compilation string in `dub` and 
`dmd` look like correctly?

```d
import vibe.d;

shared static this()
{
   auto settings = new HTTPServerSettings;
   settings.port = 8080;
   settings.bindAddresses = ["::1", "127.0.0.1"];
   listenHTTP(settings, &hello);

   logInfo("Please open http://127.0.0.1:8080/ in your browser.");
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
   res.writeBody("Hello, World!");
}
```
Apr 05 2023
next sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Wednesday, 5 April 2023 at 09:19:17 UTC, Alexander Zhirov 
wrote:
 How to compile the example given in the book correctly? When 
 compiling, an error occurs that the main function is missing. 
 If I replace `shared static this()` with `void main()', then 
 everything starts. What does the compilation string in `dub` 
 and `dmd` look like correctly?

 ```d
 import vibe.d;

 shared static this()
 {
   auto settings = new HTTPServerSettings;
   settings.port = 8080;
   settings.bindAddresses = ["::1", "127.0.0.1"];
   listenHTTP(settings, &hello);

   logInfo("Please open http://127.0.0.1:8080/ in your 
 browser.");
 }

 void hello(HTTPServerRequest req, HTTPServerResponse res)
 {
   res.writeBody("Hello, World!");
 }
 ```
This seems to work for me: ```d /+ dub.json: { "name": "test", "dependencies": { "vibe-d": "*" } } +/ import vibe.d; shared static this() { auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; listenHTTP(settings, &hello); logInfo("Please open http://127.0.0.1:8080/ in your browser."); } void main() { runApplication; } void hello(HTTPServerRequest req, HTTPServerResponse res) { res.writeBody("Hello, World!"); } ``` Run with ``` dub run --single test.d ```
Apr 05 2023
prev sibling parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
Originally vibe.d included its own main function by default. That is now 
opt-in instead of opt-out. Circa 2016.

You can either add the version ``VibeDefaultMain`` or use a main 
function and call ``runApplication`` yourself.

https://vibed.org/docs#first-steps
Apr 05 2023