如何在 c 程序中使用 dialog.h [英] How to use dialog.h in a c program

查看:118
本文介绍了如何在 c 程序中使用 dialog.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use dialog.hin my C program. For this I looked up the manual (man 3 dialog) and used the example code, which they have provided. This is how my C program look like (it is called 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.所以我已经安装了两个库,其中包含所需的头文件.

After research I figured out, that the dialog program is based on ncurses. So I have installed both libraries, which contains the required header files.

我在 Debian 上工作,所以:apt 安装对话框 libncurses5-dev libncursesw5-dev

I am working on Debian so: apt install dialog libncurses5-dev libncursesw5-dev

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

In the next step I have called the compiler, and I have also linked the libraries: 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中定义.

After research I found out that the missing references to some functions like win_wch, wunctrl, etc. are all defined in curses.h.

关于这篇文章 (使用 ncurses lib 编译 C 时 -lcurses 和 -lncurses 有什么区别?) ncursescurses 由于 curses 而相同链接到 ncurses.

Regarding to this post (What's the difference between -lcurses and -lncurses when compiling C using ncurses lib?) ncurses and curses are the same due curses is linked to ncurses.

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

But anyway, I have tried to compile with the curses library too: gcc main.c -lcurses -lncurses -ldialog. But it did not work either.

我错过了什么?为什么编译失败?

What am I missing? Why did compiling fail?

推荐答案

我在基于 Debian 的系统(Beaglebone)上进行了一些测试,但这并不是一个明显的修复.结果发现 ncurses 库有多个版本,对话库是用一个构建的,而不是您使用的那个.

I did some testing on a Debian-based system (a Beaglebone), and this was not an obvious fix. Turns out there are multiple versions of the ncurses library, and the dialog library was built with one of them, just not the one you were using.

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

The way you're supposed to figure this out is with the dialog-config command, which has options to show which CFLAGS or libraries you need on the compile line (or in the makefile), but I didn't find it on my system, so I looked at /usr/include/dlg_config.h for some clues:

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

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

Hmmm, this suggests it needs -lncursesw instead of -lncurses, so we see this compile:

$ 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 使它越过目标线:

Ok, so a lot closer: sqrt() is in the math library, so adding -lm gets it over the goal line:

$ gcc dlg.c -ldialog -lncursesw -lm

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

It seems that ncursesw is a wide-character version that works with an international character set, which was new to me.

详细说明对话框配置"内容:dialog(3) 手册页中记录了您应该执行此操作的方式:

Elaborating on the "dialog-config" stuff: The way you're supposed to do this is documented in the dialog(3) manual page:

gcc $(dialog-config --cflags) file ... $(dialog-config --libs)

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

The idea is that a package can publish what it needs to build in a command, so in this case I imagine that dialog-config --cflags might not output anything, but dialog-config --libs would output -ldialog -lncursesw -lm, so you could embed this in your makefile and have it do the right thing.

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

This paradigm is common, and on my system I see (for example) /usr/bin/python-config that shows how it was built on this machine:

$ 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 

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

If you were trying to build some kind of Python plugin, guessing the above parameters would be a real challenge.

这篇关于如何在 c 程序中使用 dialog.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆