为什么 linux 上的 curses 给我以下错误? [英] Why is curses on linux giving me following error?

查看:22
本文介绍了为什么 linux 上的 curses 给我以下错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让 getch() 工作以捕获按键.

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

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

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

给我以下错误:-

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

推荐答案

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

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

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

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

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

这篇关于为什么 linux 上的 curses 给我以下错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆