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

查看:74
本文介绍了为什么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天全站免登陆