【原创(linux)】基于curses的一个简单例程

    最近在linxu下写了一个curses的一个应用程序,感觉不错。使用起来比较方便。大部分都是调用curses的库函数。下面是我使用的例子源码:

#include     <stdlib.h>

#include     <curses.h>

#include     <sys/types.h> 

#include     <sys/stat.h>  

#include     <fcntl.h>

#include     <errno.h>

 

 

#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 即可看到所有执行的结果

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值