分类: LINUX
2010-11-29 10:17:47
#include
#include
#include
#include
#include
#include
#define StartX 1
#define StartY 1
void initial(void)
{
initscr();
cbreak();
nonl();
noecho();
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
refresh();
}
int main(int argc, char **argv)
{
int x=StartX;
int y=StartY;
int ch;
initial();
box(stdscr,'|','-');
attron(A_REVERSE);
mvaddstr(0,40,"My Curses Program");
attroff(A_REVERSE);
move(x,y);
do {
ch=getch();
switch(ch) {
case KEY_UP:
--y;
break;
case KEY_DOWN:
++y;
break;
case KEY_RIGHT:
++x;
break;
case KEY_LEFT:
--x;
break;
case '\r':
++y;
x=1;
break;
case '\t':
x+=7;
break;
case 8://判断是否 BACKSPACE
mvaddch(y,--x,' ');
break;
case 27://[ESC]键
endwin();
exit(1);
break;
default:
addch(ch);
x++;
if(x == 99) {
x=1;
y++;
}
break;
}
move(y,x);
} while (1);
exit(1);
}
以上程序存储为test.c文件;
Makefile文件的编写:
#all : test.c
test : test.c
gcc $^ -o $@ -I/usr/include -L/usr/lib -lncurses
clean :
-rm *.o
注意紫色部分代码,若不添加其代码会出现一下错误信息
gcc test.c -o test
/tmp/ccKYmGQf.o: In function `initial':
test.c:(.text+0x7): undefined reference to `initscr'
test.c:(.text+0xc): undefined reference to `cbreak'
test.c:(.text+0x11): undefined reference to `nonl'
test.c:(.text+0x16): undefined reference to `noecho'
test.c:(.text+0x1b): undefined reference to `stdscr'
test.c:(.text+0x2b): undefined reference to `intrflush'
test.c:(.text+0x30): undefined reference to `stdscr'
test.c:(.text+0x40): undefined reference to `keypad'
test.c:(.text+0x45): undefined reference to `stdscr'
test.c:(.text+0x4d): undefined reference to `wrefresh'
/tmp/ccKYmGQf.o: In function `main':
test.c:(.text+0x78): undefined reference to `stdscr'
test.c:(.text+0xc0): undefined reference to `wborder'
test.c:(.text+0xc5): undefined reference to `stdscr'
test.c:(.text+0xdd): undefined reference to `wattr_on'
test.c:(.text+0xe2): undefined reference to `stdscr'
test.c:(.text+0xfa): undefined reference to `wmove'
test.c:(.text+0x104): undefined reference to `stdscr'
test.c:(.text+0x11c): undefined reference to `waddnstr'
test.c:(.text+0x121): undefined reference to `stdscr'
test.c:(.text+0x139): undefined reference to `wattr_off'
test.c:(.text+0x13f): undefined reference to `stdscr'
test.c:(.text+0x155): undefined reference to `wmove'
test.c:(.text+0x15a): undefined reference to `stdscr'
test.c:(.text+0x162): undefined reference to `wgetch'
test.c:(.text+0x20e): undefined reference to `stdscr'
test.c:(.text+0x224): undefined reference to `wmove'
test.c:(.text+0x22e): undefined reference to `stdscr'
test.c:(.text+0x23e): undefined reference to `waddch'
test.c:(.text+0x245): undefined reference to `endwin'
test.c:(.text+0x25a): undefined reference to `stdscr'
test.c:(.text+0x266): undefined reference to `waddch'
test.c:(.text+0x280): undefined reference to `stdscr'
test.c:(.text+0x296): undefined reference to `wmove'
collect2: ld returned 1 exit status
make: *** [test] Error 1
当出现上述错误信息的时候,需要在Makefile文件中编译的时候添加以下语句
# gcc test.c -o test -I/usr/include -L/usr/lib -lncurses
最后执行./test 即可看到所有执行的结果
chinaunix网友2010-12-01 15:15:53
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com