如何在c程序中使用dialog.h

问题描述 投票:0回答:1

我正在尝试在C程序中使用dialog.h。为此,我查阅了手册(man 3 dialog),并使用了他们提供的示例代码。这就是我的C程序的样子(它称为main.c):

#include <dialog.h>

int main(void)
{
  int status;
  init_dialog(stdin, stdout);
  status = dialog_yesno(
    "Hello, in dialog-format",
    "Hello World!",
    0, 0);
  end_dialog();
  return status;
}

经过研究,我发现dialog程序基于ncurses。因此,我已经安装了两个库,其中都包含必需的头文件。

我正在研究Debian,因此:apt install dialog libncurses5-dev libncursesw5-dev

在下一步中,我调用了编译器,并且还链接了库:gcc main.c -ldialog -lncurses


但是编译没有成功。

/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(util.o): In function `dlg_auto_size':
(.text+0x1a06): undefined reference to `sqrt'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(trace.o): In function `dlg_trace_win':
(.text+0x29c): undefined reference to `win_wch'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(trace.o): In function `dlg_trace_win':
(.text+0x2ab): undefined reference to `wunctrl'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(arrows.o): In function `dlg_draw_arrows2':
(.text+0x2c4): undefined reference to `_nc_wacs'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(arrows.o): In function `dlg_draw_arrows2':
(.text+0x2d6): undefined reference to `wadd_wch'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(arrows.o): In function `dlg_draw_arrows2':
(.text+0x43c): undefined reference to `_nc_wacs'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(arrows.o): In function `dlg_draw_arrows2':
(.text+0x44e): undefined reference to `wadd_wch'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(arrows.o): In function `dlg_draw_scrollbar':
(.text+0x878): undefined reference to `_nc_wacs'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(arrows.o): In function `dlg_draw_scrollbar':
(.text+0x88f): undefined reference to `wvline_set'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(inputstr.o): In function `dlg_index_columns':
(.text+0x932): undefined reference to `setcchar'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(inputstr.o): In function `dlg_index_columns':
(.text+0x93c): undefined reference to `wunctrl'
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libdialog.a(ui_getc.o): In function `dlg_getc':
(.text+0x603): undefined reference to `wget_wch'
collect2: error: ld returned 1 exit status

[研究后,我发现win_wchwunctrl等某些功能的缺少引用均在curses.h中定义。

关于此帖子(What's the difference between -lcurses and -lncurses when compiling C using ncurses lib?ncursescurses相同,因为curses链接到ncurses

但是无论如何,我也尝试使用curses库进行编译:gcc main.c -lcurses -lncurses -ldialog。但这也不起作用。

我想念的是什么?为什么编译失败?

c linux dialog ncurses curses
1个回答
0
投票

我在基于Debian的系统(Beaglebone)上进行了一些测试,但这并不是一个明显的解决方案。事实证明,ncurses库有多个版本,而对话框库是使用其中的一个构建的,而不是您所使用的版本。

您应该使用dialog-config命令来解决这个问题,该命令具有一些选项,可以显示您在编译行(或在makefile中)所需的CFLAGS或库,但是我没有找到它我的系统,所以我查看了/usr/include/dlg_config.h的一些线索:

...
#define DLG_HAVE_LIBINTL_H 1
#define DLG_HAVE_LIBNCURSESW 1   <---
#define DLG_HAVE_LIMITS_H 1

[嗯,这表明它需要-lncursesw而不是-lncurses,所以我们看到此编译:

$ gcc dlg.c -ldialog -lncursesw
/usr/bin/ld: /usr/lib/gcc/arm-linux-gnueabihf/8/../../../arm-linux-gnueabihf/libdialog.a(util.o): in function `dlg_auto_size':
(.text+0x13d2): undefined reference to `sqrt'

好吧,更近了:sqrt()在数学库中,因此添加-lm会使它超出目标线:

$ gcc dlg.c -ldialog -lncursesw -lm

[ncursesw似乎是宽字符版本,可以使用国际字符集,这对我来说是新的。

© www.soinside.com 2019 - 2024. All rights reserved.