I am trying to teach myself AI using neural networks. Long story short, I wanted to create a simple graphic that would display what is happening in my program using ncurses. The tutorial that I am using is found here.
I was under the impression that D was compatible with C and I could theoretically call C functions relatively easily.
I find that not to be the case. I am a relatively novice programmer, so even the simplistic explanations are a little above my head. I found this here.
D is designed to fit comfortably with a C compiler for the target system. D makes up for not having its own VM by relying on the target environment's C runtime library. It would be senseless to attempt to port to D or write D wrappers for the vast array of C APIs available. How much easier it is to just call them directly.
This is done by matching the C compiler's data types, layouts, and function call/return sequences.
That sounds wonderful. A little bit over my head. I tested and got a simple C program working:
#include <curses.h>
int main(void) {
int ch;
initscr();
noecho();
cbreak();
printw("Hit Ctrl+C to exit ...\n\n");
for (;;) {
ch = getch();
printw("Value of char: %d (%02x)\n", ch, ch);
}
endwin();
return 0;
}
shamelessly copied and pasted from another question on SO. At least I did my homework.
I tried basically the same thing from a simple D program. I got this error:
Error: module curses is in file 'curses.d' which cannot be read
I am absolutely positive that I am trying something really stupid.
Is there an easy way to use ncurses in a D program?
I'm running on zero sleep and caffeine, so please be gentle! Even a link to a website would be greatly appreciated!
I probably didn't include everything that I should have, so AMA.
And feel free to insult my intelligence.
D has been successfully used for AAA games, language interpreters, virtual machines, an operating system kernel, GPU programming, web development, numerical analysis, GUI applications, a passenger information system, machine learning, text processing, web and application servers and research.
ncurses (new curses) is a programming library providing an application programming interface (API) that allows the programmer to write text-based user interfaces (TUI) in a terminal-independent manner. It is a toolkit for developing "GUI-like" application software that runs under a terminal emulator.
The curses library was originally written for BSD Unix; the later System V versions of Unix from AT&T added many enhancements and new functions. BSD curses is no longer maintained, having been replaced by ncurses, which is an open-source implementation of the AT&T interface.
To create menus, you first create items, and then post the menu to the display. After that, all the processing of user responses is done in an elegant function menu_driver() which is the work horse of any menu program. The general flow of control of a menu program looks like this. Create items using new_item().
Ok, after about 8 hours of digging through this crap I have determined that it is indeed possible to call C functions natively.
HOWEVER, it is also stated that "It would be senseless to attempt to port to D or write D wrappers for the vast array of C APIs available. How much easier it is to just call them directly."
Yeah, I'm going to call BS on that one. You DO have to port to D. Is it not considered porting when you are going from a macro enabled preprocessing .h file to a .d file? It is definitely nontrivial. So in my opinion they are intentionally leaving out the hard part and trying to make it look way better than it actually is.
In case anyone is wondering, If you have a C api that you would like to call in your D code: go grab the header file and attempt to convert it to something that D can read. Then simply compile your code, importing your new .d file, and linking it with whatever you are interfacing with. If you did it right, it'll work and you'll now have lots of memory leaks.
In my opinion, unless you need the entire library, save yourself a headache and just link a small C wrapper to your D code. You grab only what you need, and you have the added benefit of being able to rename stuff to whatever you want.
There are a couple of projects to help automate the process of translating header files. dtoh for windows only, and bcd which is found on dsource. bcd also includes bindings for curses! they are listed as being alpha only, but they seem to be working. I'm trying to call their curses.d file from my main.d file and I've been getting:
main.d:13: Error: cannot implicitly convert expression ("ype any character to see it in bold\x0a") of type string to char[]
main.d:15: Error: function aphrodite.curses.printw (char*,...) is not callable using argument types (char[])
main.d:15: Error: cannot implicitly convert expression (stuff) of type char[] to char*
main.d:20: Error: function aphrodite.curses.printw (char*,...) is not callable using argument types (string)
main.d:20: Error: cannot implicitly convert expression ("F1 Key pressed") of type string to char*
main.d:26: Error: function aphrodite.curses.printw (char*,...) is not callable using argument types (string)
main.d:26: Error: cannot implicitly convert expression ("The pressed key is ") of type string to char*
main.d:28: Error: function aphrodite.curses.printw (char*,...) is not callable using argument types (string,int)
main.d:28: Error: cannot implicitly convert expression ("%c") of type string to char*
so my problem lies in the way C handles strings and the way D handles strings. They don't match up and my extremely limited C knowledge doesn't tell me how to fix it. Luckily, for all the anti-documentation about calling C functions, there is quite a bit of info about how D types translate to C types.
I sincerely hope that someone finds this helpful somewhere down the line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With