ncurses api与D编程语言。

时间:2021-12-24 04:50:50

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.

我尝试用神经网络自学人工智能。长话短说,我想创建一个简单的图形来显示我的程序使用ncurses所发生的事情。我使用的教程是在这里找到的。

I was under the impression that D was compatible with C and I could theoretically call C functions relatively easily.

我觉得D和C是兼容的,理论上我可以相对容易地调用C函数。

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.

D被设计成适合于目标系统的C编译器。通过依赖目标环境的C运行时库,D弥补了没有自己的VM。试图将大量的C api移植到D或写D包装器是毫无意义的。直接调用它们会容易得多。

This is done by matching the C compiler's data types, layouts, and function call/return sequences.

这是通过匹配C编译器的数据类型、布局和函数调用/返回序列来完成的。

That sounds wonderful. A little bit over my head. I tested and got a simple C program working:

这听起来太棒了。有点过头了。我测试并得到一个简单的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;
}

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:

我从一个简单的D程序中尝试了基本相同的东西。我得到这个错误:

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?

在D程序中是否有使用ncurses的简单方法?

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.

我可能没有包括所有我应该拥有的东西,所以AMA。

And feel free to insult my intelligence.

你可以随意侮辱我的智商。

4 个解决方案

#1


4  

Ok, after about 8 hours of digging through this crap I have determined that it is indeed possible to call C functions natively.

好吧,经过8个小时的挖掘,我已经确定了,我们确实可以调用C函数。

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."

然而,也有人说,“试图将大量的C api移植到D或写D包装上是毫无意义的。”直接给他们打电话要容易得多。

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.

是的,我要叫它b。您必须将端口传输到d。当您从一个宏启动的预处理.h文件到.d文件时,它不考虑移植吗?这绝对是重要的。所以在我看来,他们是故意把困难的部分排除在外,试图让它看起来比实际情况更好。

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.

如果有人想知道,如果您有一个您想在D代码中调用的C api:去抓取头文件并尝试将其转换为D可以读取的内容。然后简单地编译代码,导入新的.d文件,并将其与您所连接的任何内容连接起来。如果你做对了,它会起作用,现在你会有很多内存泄漏。

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.

在我看来,除非您需要整个库,否则只会让您自己头疼,并将一个小的C包装器链接到您的D代码中。你只抓取你需要的东西,而且你还有额外的好处,可以把东西重命名为你想要的东西。

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:

有几个项目可以帮助自动化翻译头文件的过程。dtoh只适用于windows,而bcd则在dsource中找到。bcd还包含了诅咒的绑定!他们只被列为阿尔法,但他们似乎在工作。我试着叫他们的诅咒。从我的主要文件。d文件,我得到了:

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.

我的问题在于C处理字符串和D处理字符串的方式。它们不匹配,而我的有限的C知识并没有告诉我如何修复它。幸运的是,对于所有关于调用C函数的反文档,有很多关于D类型如何转换为C类型的信息。

I sincerely hope that someone finds this helpful somewhere down the line.

我真心希望有人能在这一行找到这样的帮助。

#2


3  

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.

我发现一个项目名字叫ycurses。我发现TLS的问题是针对D2的。我修改了文件和D2一起工作。dmd, gdc,一切正常。我终于可以用D了!它只是花了一个漫长的周末的马拉松编码和研究。

Since the code I found is old and appears to be abandoned, I am now hosting it on github

由于我找到的代码是旧的并且似乎被抛弃了,所以我现在把它托管在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.

代码中包含了一个很好的教程,以及如何链接的指导。你的欢迎。我突然觉得很有成就感。

#3


1  

Ok, there is a curses.d port? I don't know what to call it.. It is located here.

好吧,有一种诅咒。d端口?我不知道该怎么称呼它。它坐落在这里。

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.

它仍然要求您链接到ncurses库,但是如果您使用d1 dmd编译器编译,它的工作非常完美。这似乎是无用的在我目前的项目中,我要么去港口的文件d2(这个项目不是一个坏主意,早已被抛弃)或是否有一种链接d1编译文件,d2编译文件和C。这似乎我直白,但我也认为链接到C会简单。

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.

因此,经过几天的反复测试,使用7年前的文件,我终于从互联网上找到了一个使用curses库编译的简单的hellp世界。

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...

我正在考虑将dcurses文件移植到D2,并亲自托管它们……我只是希望我能有更多这样的经历……

#4


-3  

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).

我可以建议您看一下Python吗?我知道这不是D,是一种完全不同的语言,但如果你的目标是学习人工智能,而不是坚持使用D,那么Python就很好。它会让你在1/10的时间里做你通常会做的事情。Python中的ncurses是一种轻松。我认为有一个人写了一个俄罗斯方块的游戏大约55行(这是标准的)。

#1


4  

Ok, after about 8 hours of digging through this crap I have determined that it is indeed possible to call C functions natively.

好吧,经过8个小时的挖掘,我已经确定了,我们确实可以调用C函数。

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."

然而,也有人说,“试图将大量的C api移植到D或写D包装上是毫无意义的。”直接给他们打电话要容易得多。

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.

是的,我要叫它b。您必须将端口传输到d。当您从一个宏启动的预处理.h文件到.d文件时,它不考虑移植吗?这绝对是重要的。所以在我看来,他们是故意把困难的部分排除在外,试图让它看起来比实际情况更好。

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.

如果有人想知道,如果您有一个您想在D代码中调用的C api:去抓取头文件并尝试将其转换为D可以读取的内容。然后简单地编译代码,导入新的.d文件,并将其与您所连接的任何内容连接起来。如果你做对了,它会起作用,现在你会有很多内存泄漏。

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.

在我看来,除非您需要整个库,否则只会让您自己头疼,并将一个小的C包装器链接到您的D代码中。你只抓取你需要的东西,而且你还有额外的好处,可以把东西重命名为你想要的东西。

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:

有几个项目可以帮助自动化翻译头文件的过程。dtoh只适用于windows,而bcd则在dsource中找到。bcd还包含了诅咒的绑定!他们只被列为阿尔法,但他们似乎在工作。我试着叫他们的诅咒。从我的主要文件。d文件,我得到了:

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.

我的问题在于C处理字符串和D处理字符串的方式。它们不匹配,而我的有限的C知识并没有告诉我如何修复它。幸运的是,对于所有关于调用C函数的反文档,有很多关于D类型如何转换为C类型的信息。

I sincerely hope that someone finds this helpful somewhere down the line.

我真心希望有人能在这一行找到这样的帮助。

#2


3  

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.

我发现一个项目名字叫ycurses。我发现TLS的问题是针对D2的。我修改了文件和D2一起工作。dmd, gdc,一切正常。我终于可以用D了!它只是花了一个漫长的周末的马拉松编码和研究。

Since the code I found is old and appears to be abandoned, I am now hosting it on github

由于我找到的代码是旧的并且似乎被抛弃了,所以我现在把它托管在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.

代码中包含了一个很好的教程,以及如何链接的指导。你的欢迎。我突然觉得很有成就感。

#3


1  

Ok, there is a curses.d port? I don't know what to call it.. It is located here.

好吧,有一种诅咒。d端口?我不知道该怎么称呼它。它坐落在这里。

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.

它仍然要求您链接到ncurses库,但是如果您使用d1 dmd编译器编译,它的工作非常完美。这似乎是无用的在我目前的项目中,我要么去港口的文件d2(这个项目不是一个坏主意,早已被抛弃)或是否有一种链接d1编译文件,d2编译文件和C。这似乎我直白,但我也认为链接到C会简单。

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.

因此,经过几天的反复测试,使用7年前的文件,我终于从互联网上找到了一个使用curses库编译的简单的hellp世界。

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...

我正在考虑将dcurses文件移植到D2,并亲自托管它们……我只是希望我能有更多这样的经历……

#4


-3  

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).

我可以建议您看一下Python吗?我知道这不是D,是一种完全不同的语言,但如果你的目标是学习人工智能,而不是坚持使用D,那么Python就很好。它会让你在1/10的时间里做你通常会做的事情。Python中的ncurses是一种轻松。我认为有一个人写了一个俄罗斯方块的游戏大约55行(这是标准的)。