gpt4 book ai didi

c - 使用 curses.h 从键盘获取字符

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:23 24 4
gpt4 key购买 nike

我正在尝试使用 curses.h 从键盘获取一个字符。这是我的来源 (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;
}

我使用以下命令进行编译:

gcc get_char_example.c -o get_char_example

我收到以下错误:

/tmp/ccjaXwRU.o: 在函数“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

使用:海湾合作委员会(海湾合作委员会)7.2.0Arch Linux 4.13.9-1-ARCH

提前致谢!

最佳答案

这是一个链接器错误(参见 this,几乎是重复的)。 ncurses 众所周知 pkg-config (这会在需要时提供更多依赖项),因此请尝试编译:

 gcc -Wall -Wextra -g \
$(pkg-config --cflags ncurses) \
get_char_example.c \
$(pkg-config --libs ncurses)

您想Invoke GCC包含所有警告和调试信息;你会use GDB ,可能在 emacs 下或使用 ddd,用于调试。

更好的是,使用一些 build automation工具(如 makeninja)。用GNU makethis 中汲取灵感写下你的Makefile (其中 tab-s 很重要)。

在我的 Debian Sid 上,pkg-config --cflags ncurses 提供 -D_GNU_SOURCE -D_DEFAULT_SOURCEpkg-config --libs ncurses 提供-lncurses -ltinfo,所以简单地与 -lncurses 链接是不够的(这就是为什么 this 在今天2017 年 11 月;几年前使用简单的 -lncurses 链接就足够了,今天则不然)。

使用pkg-config --list-all 查找pkg-config 已知的所有包和库;在开发您的 库时,请考虑为其提供一些.pc 文件。

顺便说一句,您的程序应该首先使用 initscr 进行初始化并使用 printw;阅读Ncurses-programming-HowTo即使它有点旧。

另请注意,在 21st 世纪使用 ASCII 已过时。 程序和计算机使用UTF-8 everywhere ,这会产生深远的影响,您应该意识到这一点(因为 UTF-8 字符可以跨越 几个 字节)。我的法语键盘有 ²§é 键(即使是美式键盘,您也可以粘贴它们) 而这些都不是 ASCII 格式的。还可以考虑使用一些 UTF-8 库,例如 libunistring .

关于c - 使用 curses.h 从键盘获取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47060311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com