Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1204614
  • 博文数量: 232
  • 博客积分: 7563
  • 博客等级: 少将
  • 技术积分: 1930
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-21 11:17
文章分类

全部博文(232)

文章存档

2011年(17)

2010年(90)

2009年(66)

2008年(59)

分类: LINUX

2010-11-29 10:17:47

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

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

阅读(6388) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

dingdingbob2012-03-26 19:01:34

你好,请问用什么命令系统在链接时自动找到这些库?

chinaunix网友2010-12-01 15:15:53

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com