为什么 Linux 上的 Curses 会出现以下错误?

发布于 2024-08-06 18:49:11 字数 298 浏览 8 评论 0原文

尝试让 getch() 捕获按键操作。

    #include <curses.h>
    ...
    ...
    WINDOW *w;
    char f;

   w = initscr();
   timeout(3000);
   f = getch();
   endwin();

给我以下错误:-

undefined reference to `wgetch'
undefined reference to `stdscr'

Trying to get getch() working to capture key press.

    #include <curses.h>
    ...
    ...
    WINDOW *w;
    char f;

   w = initscr();
   timeout(3000);
   f = getch();
   endwin();

is giving me following error:-

undefined reference to `wgetch'
undefined reference to `stdscr'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海风掠过北极光 2024-08-13 18:49:11

这是一个链接错误。您是否正确链接到curses库?

在 C 中使用库涉及两个步骤。

  1. 您从源文件中#include 相关的头文件。这样您的代码就知道库函数的签名是什么。所以你这样做是正确的。
  2. 编译代码时,您需要告诉链接器链接到相关库,以便它可以找到这些函数的定义。 这不是你做的事情。假设您使用的是 gcc,那么将 -lncurses 添加到编译行就可以了。这是链接说明

That's a linking error. Are you linking to the curses library correctly?

There are two steps involved in using a library in C.

  1. You #include the relevant header files from your source files. This is so your code knows what signatures of the library functions are. So you're doing this correctly.
  2. When compiling your code, you need to tell the linker to link to the relevant libraries, so it can find the definition of those functions. This is what you're not doing. Assuming you're using gcc then adding -lncurses to the compile line should do it. Here's an explanation of linking.
初心未许 2024-08-13 18:49:11

上面的答案是正确的,但格式是:

gcc -Wall program.c -o name_of_binary -lncurses

当我这样做时:

gcc -Wall -lncurses program.c...

它不起作用,所以显然应该将其钉在最后。

The above answers are correct but the format is:

gcc -Wall program.c -o name_of_binary -lncurses

When I did:

gcc -Wall -lncurses program.c...

It did not work so apparently it should be tacked on the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文