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

css*_*vin 2 c linux curses dialog ncurses

dialog.h我正在尝试在我的 C 程序中使用。为此,我查阅了手册(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;
}
Run Code Online (Sandbox Code Playgroud)

经过研究,我发现该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
Run Code Online (Sandbox Code Playgroud)

经过研究,我发现缺少对某些函数的引用win_wch,如wunctrl、 等,均在curses.h.

关于这篇文章(What's the Difference between -lcurses and -lncurses when compiling C using ncurses lib?ncurses并且curses由于curses链接到ncurses.

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

我缺少什么?为什么编译失败?

Ste*_*edl 6

我在基于 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
Run Code Online (Sandbox Code Playgroud)

嗯,这表明它需要-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'
Run Code Online (Sandbox Code Playgroud)

好吧,更接近了:sqrt()在数学库中,所以添加-lm它就可以超过目标线:

$ gcc dlg.c -ldialog -lncursesw -lm
Run Code Online (Sandbox Code Playgroud)

这似乎ncursesw是一个适用于国际字符集的宽字符版本,这对我来说是新的。

编辑:详细说明“dialog-config”内容:您应该执行此操作的方式记录在dialog(3)手册页中:

gcc $(dialog-config --cflags) file ... $(dialog-config --libs)
Run Code Online (Sandbox Code Playgroud)

这个想法是,一个包可以发布在命令中构建所需的内容,因此在这种情况下,我想可能dialog-config --cflags不会输出任何内容,但dialog-config --libs会输出-ldialog -lncursesw -lm,因此您可以将其嵌入到 makefile 中并让它做正确的事情。

这种范例很常见,在我的系统上,我看到(例如)/usr/bin/python-config它显示了它是如何在这台机器上构建的:

$ python-config --cflags
-I/usr/include/python2.7 -I/usr/include/arm-linux-gnueabihf/python2.7
-fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g
-fdebug-prefix-map=/build/python2.7-RT6aMn/python2.7-2.7.16=.
-fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG
-g -fwrapv -O2 -Wall -Wstrict-prototypes

$ python-config --libs
-lpython2.7 -lpthread -ldl  -lutil -lm 
Run Code Online (Sandbox Code Playgroud)

如果您尝试构建某种 Python 插件,猜测上述参数将是一个真正的挑战。