$ dub init
dub.json
file. To compile, build and run heloworld from the app.d
file, just run the dub
command.dub
utility uses the package registry at the following address . The use of libraries written with C / C ++ is also possible, but this requires the inclusion of special packages in the project, called binding . The library itself must be installed in the system. For example, libev , which will continue to be used, is installed on debian-like linux distributions as follows: $ sudo apt-get install libev-dev
dub.json
file in dub.json
to use libev
in the project. It all comes down to adding to the dependencies section of the dub.json
file the dub.json
name, version and, if necessary, some other parameters.dub
command, all the packages listed in dependencies will be checked and loaded if they are absent.libev
from libev
. Below is the code that should be placed in the app.d
file, and if everything went smoothly, we will get an executable file that will delight every second with the message “Hello, World!” In the console. import std.stdio; import deimos.ev; void main() { ev_timer watcher; extern(C) void cb_timer(ev_loop_t* loop, ev_timer* watcher, int revents) { writeln("Hello, World!"); } auto p_loop = ev_loop_new(EVFLAG_AUTO); ev_timer_init(&watcher, &cb_timer, 1.0, 1.0); ev_timer_start(p_loop, &watcher); ev_run(p_loop, 0); }
libev
. Secondly, the callback function cb_timer
defined as extern(C)
. In fact, its call will come from the depths of the connected library, and we have it written in C. Consequently, the cb_timer
calling cb_timer
must correspond to the call of functions written in C.cb_timer
in the body of the main
function, the code differs little from the similar C program. This is due to the fact that the binding packages usually contain minimal binding on function calls from the ssh library. Often, for convenience, another level of binding is made that provides more readable code using the “syntactic sugar” of the D language. For example, similar code could look something like this. import std.stdio; import mercury.core; void main() { new TimerWatcher(1.0, 1.0, (revents) { writeln("Hello, World!"); }).start(); defaultEventLoop.start(); }
libev
using a parameterized class for watchers. It turned out like not bad. If this is interesting to someone - in the next article I will tell about it.dub
, the author described in some detail the main features of the utility.Source: https://habr.com/ru/post/226301/
All Articles