curses库是一组函数,可以用它来设置光标的位置和终端屏幕上显示的字符样式。curses库将终端屏幕看成是由字符单元组成的网格,每一个单元由(行、列)坐标对标示。坐标原点是屏幕的左上角,行坐标自上而下递增,列坐标自左而右递增。
这里介绍几个常用函数:
initscr() 初始化curses库和tty
endwin() 关闭curses库并重置tty
refresh() 使屏幕按照自己的意图显示
move(r,s) 移动光标到屏幕的(r,s)位置
addstr(s) 在当前位置画字符串s
addch(c) 在当前位置画字符c
clear() 清屏
standout() 启动standout模式(一般使屏幕反色)
standend() 关闭standout模式
一个简单的curses库程序curses1.c:
#include <stdio.h> #include <curses.h> main(){ initscr(); clear(); move(10,20); addstr("Hello,world"); move(LINES-1,0); //LINES是当前终端的行数 refresh(); getch(); endwin(); }
$gcc -o curese curses.c
编译报错如下:
error: curses.h: No such file or directory
即缺少curses库文件,检查所依赖的软件包:
$yum provides */curses.h ... ncurses-devel-5.7-3.20090208.el6.x86_64 : Development files for the ncurses : library Repo : server Matched from: Filename : /usr/include/ncurses/curses.h Filename : /usr/include/curses.h Filename : /usr/include/ncursesw/curses.h ...
yum install ncurses-devel -y //安装该软件包即可
[[email protected] 7]# gcc -o curses1 curses1.c /tmp/cclUPDCB.o: In function `main‘: curses1.c:(.text+0x5): undefined reference to `initscr‘ curses1.c:(.text+0xc): undefined reference to `stdscr‘ curses1.c:(.text+0x14): undefined reference to `wclear‘ curses1.c:(.text+0x1b): undefined reference to `stdscr‘ curses1.c:(.text+0x2d): undefined reference to `wmove‘ curses1.c:(.text+0x34): undefined reference to `stdscr‘ curses1.c:(.text+0x46): undefined reference to `waddnstr‘ curses1.c:(.text+0x4c): undefined reference to `LINES‘ curses1.c:(.text+0x56): undefined reference to `stdscr‘ curses1.c:(.text+0x65): undefined reference to `wmove‘ curses1.c:(.text+0x6c): undefined reference to `stdscr‘ curses1.c:(.text+0x74): undefined reference to `wrefresh‘ curses1.c:(.text+0x7b): undefined reference to `stdscr‘ curses1.c:(.text+0x83): undefined reference to `wgetch‘ curses1.c:(.text+0x88): undefined reference to `endwin‘ collect2: ld returned 1 exit status
解决办法是在编译时加上参数-lncurses
gcc -o curses1 curses1.c -lncurses
时间: 2024-10-03 04:21:22