这是一个链接器错误(请参阅this https://stackoverflow.com/questions/1513417/why-is-curses-on-linux-giving-me-following-error,几乎是重复的)。ncurses
众所周知pkg 配置 https://www.freedesktop.org/wiki/Software/pkg-config/(这会在需要时提供更多依赖项),因此尝试使用以下命令进行编译:
gcc -Wall -Wextra -g \
$(pkg-config --cflags ncurses) \
get_char_example.c \
$(pkg-config --libs ncurses)
You want to Invoke GCC https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html with all warnings and debug info; you would use GDB https://sourceware.org/gdb/current/onlinedocs/gdb/, perhaps under emacs
or with ddd
, for debugging.
Even better, use some build automation https://en.wikipedia.org/wiki/Build_automation tool (like make https://www.gnu.org/software/make or ninja
). With GNU make https://www.gnu.org/software/make/manual/html_node/index.html take inspiration from this https://stackoverflow.com/a/20146082/841108 to write your Makefile https://en.wikipedia.org/wiki/Makefile (where tab-s are significant).
在我的 Debian Sid 上,pkg-config --cflags ncurses
gives -D_GNU_SOURCE -D_DEFAULT_SOURCE
and pkg-config --libs ncurses
gives -lncurses -ltinfo
,所以只需链接-lncurses
is not足够了(这就是为什么this https://stackoverflow.com/questions/1513417/why-is-curses-on-linux-giving-me-following-error与 2017 年 11 月的今天并不完全重复;几年前简单地链接-lncurses
已经足够了,今天还不够)。
Use pkg-config --list-all
查找所有已知的包和库pkg 配置 https://www.freedesktop.org/wiki/Software/pkg-config/;开发时your图书馆还考虑提供一些.pc
为其归档。
顺便说一句,你的程序应该首先初始化initscr
并使用printw
; Read Ncurses 编程方法 http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/即使它有点旧了。
Notice also that using ASCII in the 21st century is obsolete. Programs and computers use UTF-8 everywhere http://utf8everywhere.org/, and this has profound consequences you should be aware of (since an UTF-8 https://en.wikipedia.org/wiki/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 https://www.gnu.org/software/libunistring/manual/libunistring.html.