如何正确引用GNU readline库来扫描终端输入?

时间:2010-09-23 23:06:46

标签: c unix gnu readline

我正在尝试编译在GNU readline中使用以下内容的C代码。

#include <readline/readline.h>;
#include <readline/history.h>;

我已尝试将 &lt;&gt; 更改为 “” ,并在有和没有的情况下进行编译 -lreadline 选项。似乎没什么用。在 gcc 下编译时没有 -lreadline 会导致在编译时生成以下内容(详细):

Reading specs from /software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6/specs
Configured with: ../gcc-3.4.6/configure --prefix=/software/gcc-3.4.6-0/pkg --disable-dependency-tracking --localstatedir=/var --disable-nls --program-suffix=34 --enable-shared --enable-version-specific-runtime-libs
Thread model: posix
gcc version 3.4.6
 /software/gcc-3.4.6-0/pkg/libexec/gcc/i386-unknown-freebsd6.1/3.4.6/cc1 -quiet -v myshell.c -quiet -dumpbase myshell.c -auxbase myshell -version -o /var/tmp//ccVSq3jQ.s
ignoring nonexistent directory "/software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6/../../../../i386-unknown-freebsd6.1/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /software/gcc-3.4.6-0/pkg/include
 /software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6/include
 /usr/include
End of search list.
GNU C version 3.4.6 (i386-unknown-freebsd6.1)
        compiled by GNU C version 3.4.6.
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=129976
 as -o /var/tmp//ccl1Jaqk.o /var/tmp//ccVSq3jQ.s
 /software/gcc-3.4.6-0/pkg/libexec/gcc/i386-unknown-freebsd6.1/3.4.6/collect2 -V -dynamic-linker /libexec/ld-elf.so.1 -L/software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1 -o a /usr/lib/crt1.o /usr/lib/crti.o /software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6/crtbegin.o -L/software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6 -L/software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6/../../.. /var/tmp//ccl1Jaqk.o -lreadline -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /software/gcc-3.4.6-0/pkg/lib/gcc/i386-unknown-freebsd6.1/3.4.6/crtend.o /usr/lib/crtn.o
/usr/bin/ld: cannot find -lreadline
GNU ld version 2.15 [FreeBSD] 2004-05-23
  Supported emulations:
   elf_i386_fbsd
collect2: ld returned 1 exit status

这是在没有 -lreadline 选项进行编译时的输出。

/var/tmp//ccNnucSC.o(.text+0x4f): In function `main':
: undefined reference to `readline'
collect2: ld returned 1 exit status

到目前为止,我无法解决此错误。在我的代码中或在调用gcc时我缺少什么?

2 个答案:

答案 0 :(得分:10)

在UNIX下编译时,我发现正确引用 GNU readline 库需要以下内容:

gcc code.c -L/usr/local/lib -I/usr/local/include -lreadline

这可确保编译器在编译和链接期间找到 readline 目录和文件。

答案 1 :(得分:6)

听起来你没有安装libreadline开发库。在Debian(包括FreeBSD端口)上,库位于/ lib中,开发库(您将链接到的)位于/ usr / lib中。

此外,在你的包含之后你有分号,这是你不应该的。