D, Zero to Hello World
Why don’t more people use this?
2023/02
Dlang with vibe.d
Installed dmd using the official install script, and activated, source ~/dlang/dmd-2.101.1/activate
. On Windows I use scoop to install the dmd compiler.
Created a basic project using DUB and used the default settings, dub init [project-name]
. Note, DUB can create a vibe.d project with dub init [project-name] -t vibe.d
. DUB also initialized the project with git and added a .gitignore file.
Added the vibe.d package.
$ dub init dlang_test
$ dub add vibe-d
-> Adding dependency vibe-d ~>0.9.5
Here I got lost for a minute looking for the next step. I checked the vibe.d and DUB package docs. Then the DLang Tour and vibe.d Tour. Hoping for invalid command here is the help, I typed dub
in the terminal and it started installed the dependencies, then failed.
/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/bin/ld: cannot find -lssl: No such file or directory
/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/bin/ld: cannot find -lcrypto: No such file or directory
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
The vibe.d package docs lists libssl as a required dependency and gives and Debian example apt install libssl-devel
. Also, under the install section there is an example of just typing dub
. I am using openSUSE Tumbleweed and the package names do not sync with Debian. I insanely try again, hoping for a different result.
$ dub
-> Pre-gen Running commands for openssl
Ah openssl. I installed the libopenssl-devel package and success.
$ zypper install libopenssl-devel
-> The following 2 NEW packages are going to be installed:
libopenssl-3-devel libopenssl-devel
Then I found the DUB getting started docs, which lists:
$ dub build
$ dub run
$ dub test
I tried the run command and got this output, Edit source/app.d to start your project.
I thought duh, so I tried the build command, and got a dlang-test
executable, which output the same message. After opening the project in Codium, I realized why.
// app.d
import std.stdio;
void main()
{
writeln("Edit source/app.d to start your project.");
}
I replaced the contents with the Hello Vibe.d example
// app.d
import vibe.vibe;
void main()
{
listenHTTP("127.0.0.1:8080", (req, res) {
res.writeBody("Hello Vibe.d: " ~ req.path);
});
runApplication();
}
Built, ran, and tested with curl.
$ curl localhost:8080
-> Hello Vibe.d:
Overall project tree
├── dlang_test
│ ├── dlang-test - 47M debug, 13M release
│ ├── dub.json
│ ├── dub.selections.json
│ └── source
│ └── app.d
Building with dub build --build=release
reduced the size to 13M.
Starting from scratch and while writing these notes, I was done in less than 30 minutes.
I also installed and activated the LDC compiler, which about halved the executable size.
$ dub build --compiler=ldc2
$ dub build --build=release --compiler=ldc2
├── dlang_test
│ ├── dlang-test - 23M debug, 4.4M release
DUB listed to following transitive dependencies.
{
"fileVersion": 1,
"versions": {
"diet-ng": "1.8.1",
"eventcore": "0.9.22",
"libasync": "0.8.6",
"memutils": "1.0.4",
"mir-linux-kernel": "1.0.1",
"openssl": "3.3.0",
"stdx-allocator": "2.77.5",
"taggedalgebraic": "0.11.22",
"vibe-core": "1.22.6",
"vibe-d": "0.9.5"
}
}