Ubuntu平台基于ncurses编译程序报错: undefined reference to `initscr'

blogdaren 2018-12-30 抢沙发 78人次

问题背景:

#include <stdlib.h>
#include <curses.h>
#include <signal.h>

static void finish(int sig);

int main(int argc, char *argv[])
{
    int num = 0;

    /* initialize your non-curses data structures here */

    signal(SIGINT, finish);      /* arrange interrupts to terminate */

    initscr();      /* initialize the curses library */
    keypad(stdscr, TRUE);  /* enable keyboard mapping */
    nonl();         /* tell curses not to do NL->CR/NL on output */
    cbreak();       /* take input chars one at a time, no wait for \n */
    echo();         /* echo input - in color */

    if (has_colors())
    {
        start_color();

        /*
         * Simple color assignment, often all we need.  Color pair 0 cannot
         * be redefined.  This example uses the same value for the color
         * pair as for the foreground color, though of course that is not
         * necessary:
         */
        init_pair(1, COLOR_RED,     COLOR_BLACK);
        init_pair(2, COLOR_GREEN,   COLOR_BLACK);
        init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
        init_pair(4, COLOR_BLUE,    COLOR_BLACK);
        init_pair(5, COLOR_CYAN,    COLOR_BLACK);
        init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
        init_pair(7, COLOR_WHITE,   COLOR_BLACK);
    }

    for (;;)
    {
        int c = getch();     /* refresh, accept single keystroke of input */
        attrset(COLOR_PAIR(num % 8));
        num++;

        /* process the command keystroke */
    }

    finish(0);               /* we're done */
}

static void finish(int sig)
{
    endwin();

    /* do your non-curses wrapup here */

    exit(0);
}

问题原因:

缺少必要的 ncurses 库文件

解决方案:

安装如下所有的ncurses依赖库:

libncurses5                The ncurses shared libraries
libncurses5-dev            Development environment
libncurses5-dbg            Debugging libraries
libncursesw5               The ncurses shared libraries, with wide character support
libncursesw5-dev           Development environment for libncursesw5
libncursesw5-dbg           Debugging libraries for libncursesw5
ncurses-bin                Various small applications (clear, reset, tic, etc.)
ncurses-base               Standard terminal entries
ncurses-term               All the rest of the terminal entries

使用姿势:

# gcc hi.c -lcurses

版权声明:除非注明,本文由( blogdaren )原创,转载请保留文章出处。

本文链接:Ubuntu平台基于ncurses编译程序报错: undefined reference to `initscr'

发表评论:

您的昵称:
电子邮件:
个人主页: