我试图使用ncurses库v 5.9编译以下代码。关于Debian wheezy。 但我正在未定义参考图书馆的功能。 我从源代码安装了库。
#include <curses.h>
#include <menu.h>
#include<stdlib.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
mvprintw(LINES - 2, 0, "F1 to Exit");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}
这是错误
root@sss:/home/sss/Desktop# gcc -Wall -o mn mn.c -lncurses -lmenu
mn.c: In function ‘main’:
mn.c:21:8: warning: unused variable ‘cur_item’ [-Wunused-variable]
mn.c:58:1: warning: control reaches end of non-void function [-Wreturn-type]
/usr/lib/gcc/i486-linux-gnu/4.7/../../../../lib/libmenu.a(m_driver.o): In function `menu_driver':
m_driver.c:(.text+0x33b): undefined reference to `getmouse'
m_driver.c:(.text+0x36c): undefined reference to `wenclose'
m_driver.c:(.text+0x3b0): undefined reference to `wmouse_trafo'
m_driver.c:(.text+0x7b9): undefined reference to `wenclose'
m_driver.c:(.text+0x7f2): undefined reference to `wmouse_trafo'
/usr/lib/gcc/i486-linux-gnu/4.7/../../../../lib/libmenu.a(m_global.o): In function `_nc_Show_Menu':
m_global.c:(.text+0x78b): undefined reference to `copywin'
/usr/lib/gcc/i486-linux-gnu/4.7/../../../../lib/libmenu.a(m_post.o): In function `_nc_Post_Item':
m_post.c:(.text+0x3c): undefined reference to `wattr_on'
m_post.c:(.text+0x8e): undefined reference to `wattr_off'
m_post.c:(.text+0xce): undefined reference to `wattr_on'
m_post.c:(.text+0x276): undefined reference to `wattr_on'
m_post.c:(.text+0x3a3): undefined reference to `wattr_off'
m_post.c:(.text+0x3db): undefined reference to `wattr_off'
m_post.c:(.text+0x424): undefined reference to `wattr_on'
m_post.c:(.text+0x458): undefined reference to `wattr_on'
m_post.c:(.text+0x47e): undefined reference to `wattr_on'
m_post.c:(.text+0x4ce): undefined reference to `wattr_on'
m_post.c:(.text+0x4fe): undefined reference to `wattr_off'
m_post.c:(.text+0x51e): undefined reference to `wattr_off'
m_post.c:(.text+0x53a): undefined reference to `wattr_off'
m_post.c:(.text+0x5b6): undefined reference to `wattr_off'
/usr/lib/gcc/i486-linux-gnu/4.7/../../../../lib/libmenu.a(m_post.o): In function `_nc_Draw_Menu':
m_post.c:(.text+0x605): undefined reference to `wbkgdset'
m_post.c:(.text+0x61f): undefined reference to `wbkgdset'
m_post.c:(.text+0x69e): undefined reference to `wattr_on'
m_post.c:(.text+0x774): undefined reference to `wattr_off'
/usr/lib/gcc/i486-linux-gnu/4.7/../../../../lib/libmenu.a(m_post.o): In function `unpost_menu':
m_post.c:(.text+0xa41): undefined reference to `delwin'
m_post.c:(.text+0xa53): undefined reference to `delwin'
collect2: error: ld returned 1 exit status
答案 0 :(得分:6)
直接引自man menu
:
在你的库列表中,libmenu.a应该在libncurses.a之前;也就是说,你想说
-lmenu -lncurses
,而不是相反(通常会产生链接错误)。