使用 D 编程语言的 ncurses api
我正在尝试使用神经网络自学人工智能。 长话短说,我想创建一个简单的图形来显示使用 ncurses 的程序中发生的情况。 我正在使用的教程可以在此处找到。
我的印象是 D 与 C 兼容,理论上我可以相对容易地调用 C 函数。
我发现事实并非如此。 我是一个相对新手的程序员,所以即使是简单的解释也有点超出我的理解范围。 我在这里找到了这个。
D 旨在与目标系统的 C 编译器完美配合。 D 通过依赖目标环境的 C 运行时库来弥补没有自己的 VM。尝试移植到 D 或为大量可用的 C API 编写 D 包装器是毫无意义的。直接打电话给他们要容易得多。
这是通过匹配 C 编译器的数据类型、布局和函数调用/返回序列来完成的。
听起来很棒。有点超出我的头了。 我测试并得到了一个简单的 C 程序:
#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;
}
无耻地从 SO 上的另一个问题复制并粘贴。 至少我做了功课。
我从一个简单的 D 程序中尝试了基本上相同的事情。 我得到了这个错误:
Error: module curses is in file 'curses.d' which cannot be read
我绝对肯定我正在尝试一些非常愚蠢的事情。
有没有一种简单的方法可以在 D 程序中使用 ncurses?
我的睡眠和咖啡因为零,所以请保持温柔! 即使是网站的链接也将不胜感激!
我可能没有包含我应该包含的所有内容,所以 AMA。
并且随意侮辱我的智商。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,经过大约 8 个小时的研究,我确定确实可以本地调用 C 函数。
然而,它还指出“尝试移植到 D 或为大量可用的 C API 编写 D 包装器是毫无意义的。直接调用它们要容易得多。”
是的,我要说这件事是胡说八道。您确实必须移植到 D。当您从启用宏的预处理 .h 文件转到 .d 文件时,是否不考虑移植?这绝对是不平凡的。所以在我看来,他们故意忽略了困难的部分,并试图让它看起来比实际情况更好。
如果有人想知道,如果您想在 D 代码中调用 C api:
去获取头文件并尝试将其转换为 D 可以读取的文件。
然后只需编译您的代码,导入新的 .d 文件,并将其与您正在交互的任何内容链接起来。
如果你做得对,它会起作用,并且你现在会遇到很多内存泄漏。
在我看来,除非您需要整个库,否则只需将一个小的 C 包装器链接到您的 D 代码即可省去麻烦。您只获取所需的内容,并且可以将内容重命名为您想要的任何内容,从而获得额外的好处。
有几个项目可以帮助自动化翻译头文件的过程。
dtoh 仅适用于 Windows,而 bcd 可在 dsource 上找到。 bcd 还包括诅咒绑定!它们被列为仅 alpha,但它们似乎正在发挥作用。我试图从我的 main.d 文件中调用他们的curses.d 文件,我得到:
所以我的问题在于C 处理字符串的方式和D 处理字符串的方式。它们不匹配,而且我极其有限的 C 知识无法告诉我如何修复它。
幸运的是,对于所有有关调用 C 函数的反文档,有大量有关 D 类型如何转换为 C 类型的信息。
我真诚地希望有人会发现这对以后有帮助。
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:
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.
好吧,我觉得我有点垃圾邮件了,但我希望所有的信息将来都会有用。
我找到了一个项目名称ycurses。
我发现 TLS 问题是 D2 特有的。
我更改了文件以与 D2 一起使用。 dmd、gdc,一切正常。
我终于有了使用 D 的 ncurses!只是花了一个长周末的马拉松式编码和研究。
由于我发现的代码很旧并且似乎已被废弃,所以我
现在将其托管在 github 上
尽管代码声明它可以与 Tango 一起使用,但它不会。
我可能会在本周某个时间解决这个问题。
该代码包含一个很好的小教程,以及有关如何链接的说明。
别客气。我突然觉得很有成就感。
Ok, I feel I have been spamming a little, but I hope that all of the info will be useful in the future.
I found a project name ycurses.
I discovered the problem with TLS is specific to D2.
I changed the files to work with D2. dmd, gdc, it all works.
I finally have my ncurses using D! It just took a long weekend of marathon coding and researching.
Since the code I found is old and appears to be abandoned, I am
now hosting it on github
Even though the code states it will work with Tango, IT WILL NOT.
I'll probably fix that some time this week.
The code has a nice little tutorial included, as well as instruction on how to link.
Your welcome. I feel very accomplished all of a sudden.
好吧,有一个curses.d端口吗?我不知道该怎么称呼它..
它位于此处。
它仍然要求您链接 ncurses 库,但如果您使用 d1 dmd 编译器进行编译,它可以完美工作。这在我当前的项目中似乎有点无用,所以我要么必须将文件移植到 d2 (不是一个坏主意,这个项目早已被放弃),要么看看是否有办法链接 d1 编译文件、 d2 编译文件和 C。在我看来,这似乎是直接的,但我也认为链接到 C 也很简单。
因此,经过几天的尝试和错误式测试(使用从互联网上提取的 7 年前的文件),我终于得到了一个使用curses 库编译的简单的地狱世界。
我正在强烈考虑将 dcurses 文件移植到 D2 并自己托管它们...我只是希望我在此类事情上有更多经验...
Ok, there is a curses.d port? I don't know what to call it..
It is located here.
It still requires that you link against the ncurses library, but it works perfectly if you compile with the d1 dmd compiler. which seems to be kind of useless in my current project, so i've either got to port the files to d2 (not a bad idea, this project has long since been abandoned) or see if there is a way to link d1 compiles files, d2 compiled files and C. This would seem to me to be straight forward, but I also thought linking to C would be straightforward.
So after a few days and mostly trial-and-error style tests using files dated 7 years ago pulled from the internet, I finally got a simple hellp world compiled using the curses library.
I am strongly considering porting the dcurses files to D2 and hosting them myself... I just wish I had more experience with this type of thing...
我可以建议看一下Python吗?我知道它不是 D,是一种完全不同的语言,但如果你的目标是学习 AI 并且不坚持使用 D,那么 Python 非常好。它可以让您在 1/10 的时间内完成您在 D 中通常做的事情。 Python 中的 ncurses 很容易。我认为有人用大约 55 行(这是标准的)编写了一个俄罗斯方块游戏。
May I suggest having a look at Python? I know it's not D and is a completely different language but if your aim is to learn AI and don't insist on using D then Python is very good. It will allow you to do what you normally do in D in 1/10 of the time. ncurses in Python is an ease. I think there was some guy who wrote a tetris game in about 55 lines (which is standard).