使用curses.h从键盘获得一个字符 [英] getting a char from keyboard using curses.h
问题描述
我试图用curses.h从键盘上获得一个字符。
这是我的源代码(get_char_example.c):
#include< curses.h>
#include< stdio.h>
int main(void)
{
char ch;
printf(Enter a character:);
ch = getch();
printf(\\\
Its ASCII码是%d \\\
,ch);
返回0;
}
我使用以下命令来编译:
gcc get_char_example.c -o get_char_example
/tmp/ccjaXwRU.o:在函数`main'中:
get_char_example.c :(。text + 0x1c):对`stdscr'的未定义引用
get_char_example.c :(。text + 0x24):对`wgetch'的未定义引用
collect2:error:ld返回1退出状态
使用:gcc(GCC)7.2.0
Arch Linux 4.13.9-1-ARCH
预先感谢!
这是一个链接器错误(请参阅这,几乎是一个重复)。 pkg
gcc -Wall - Wextra -g \
$(pkg-config --cflags ncurses)\
get_char_example.c \
$(pkg-config --libs ncurses)
您想调用GCC ,提供所有警告和调试信息;您可以使用GDB ,也许可以在 emacs
或用 ddd
,以进行调试。
更好的是,使用编译自动化工具(如 make
或 ninja
)。使用 GNU make
从此获取灵感来编写您的 Makefile
(其中 tab -s是重要的)。
在我的Debian Sid中, pkg-config --cflags ncurses
给出了 -D_GNU_SOURCE -D_DEFAULT_SOURCE
和 pkg-config --libs ncurses
给出 -lncurses -ltinfo
,所以只需链接 -lncurses
不足 on-linux-giving-me-following-error>这个今天在2017年11月不完全是重复的;几年前,只需连接 -lncurses
使用 pkg-config --list-all
来查找所有包以及 pkg-config 已知的库;当开发你的库时,考虑为它提供一些 .pc
文件。
顺便说一下,你的程序应该以初始化 initscr
并使用 printw
;请阅读 Ncurses-programming-HowTo ,即使它有点旧。
还要注意,在21世纪使用ASCII已经过时了。 程序和计算机使用无处不在的UTF-8 ,而这具有深刻的后果,您应该知道(因为 UTF-8 字符可跨越<几个字节)。我的法文键盘有²
,§
,€
和é
(即使用美国键盘也可以粘贴它们),而且这些都不是ASCII码。考虑使用一些UTF-8库,如 libunistring 。
I'm trying to get a character from the keyboard using curses.h. This is my source (get_char_example.c):
#include <curses.h>
#include <stdio.h>
int main(void)
{
char ch;
printf("Enter a character: ");
ch = getch();
printf("\nIts ASCII code is %d\n", ch);
return 0;
}
I use the following command to compile:
gcc get_char_example.c -o get_char_example
And I get the following error:
/tmp/ccjaXwRU.o: In function `main':
get_char_example.c:(.text+0x1c): undefined reference to `stdscr'
get_char_example.c:(.text+0x24): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
using: gcc (GCC) 7.2.0 Arch Linux 4.13.9-1-ARCH
Thanks in advance!
It is a linker error (see this, nearly a duplicate). ncurses
is generally known to pkg-config (which gives more dependencies, when required), so try to compile with:
gcc -Wall -Wextra -g \
$(pkg-config --cflags ncurses) \
get_char_example.c \
$(pkg-config --libs ncurses)
You want to Invoke GCC with all warnings and debug info; you would use GDB, perhaps under emacs
or with ddd
, for debugging.
Even better, use some build automation tool (like make
or ninja
). With GNU make
take inspiration from this to write your Makefile
(where tab-s are significant).
On my Debian Sid, pkg-config --cflags ncurses
gives -D_GNU_SOURCE -D_DEFAULT_SOURCE
and pkg-config --libs ncurses
gives -lncurses -ltinfo
, so simply linking with -lncurses
is not enough (and that is why this is not exactly a duplicate today in November 2017; a few years ago linking with simply -lncurses
was enough, today it is not).
Use pkg-config --list-all
to find all the packages and libraries known to pkg-config; when developing your libraries consider also providing some .pc
file for it.
BTW, your program should start by initializing with initscr
and use printw
; Read Ncurses-programming-HowTo even if it is a bit old.
Notice also that using ASCII in the 21st century is obsolete. Programs and computers use UTF-8 everywhere, and this has profound consequences you should be aware of (since an UTF-8 character can span several bytes). My French keyboard has keys for ²
, §
, €
and é
(even with American keyboards your could paste them) and none of these are in ASCII. Consider also using some UTF-8 library like libunistring.
这篇关于使用curses.h从键盘获得一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!