在ubuntu 18.04上的c文件中未定义对“ readline”的引用(已安装libreadline-dev,与“ -lreadline”链接

时间:2018-11-27 20:38:51

标签: c ubuntu gcc readline

我知道Stackoverflow上有很多与此相关的主题,但是没有一种解决方案对我有帮助。我将Ubuntu 18.04与开发人员通常需要的所有东西一起使用:build-essential, libreadline-dev, ...

我在/usr/include/readline中进行了查看,但是阅读行肯定存在! gcc总是告诉我:

  

对“ readline”的未定义引用

无论我是否与“ -lreadline”链接,这种情况都是独立发生的。


修改

作为用户@Kamil Cuk已经提供了简单的解决方案,我想补充一些信息:

$ gcc -lreadline -o code code.c

可在macOS上使用Apple-gcc(使用clang)以及gnu-gcc(通过brew安装)。仅在Ubuntu上,链接器标志必须在“ code.c”之后

(也许是因为Ubuntu使用gcc-7,而在我的Mac上,我使用的是gcc-8)

编辑结束


code.c

#include <stdlib.h>
#include <stdio.h>
#include <readline/readline.h>

int main(void) {
    char * buffer = readline("enter sth: ");
    printf("%s\n", buffer);
    printf("bye!\n");
}

libreadline-dev软件包信息

dpkg -L libreadline-dev   
/.
/usr
/usr/include
/usr/include/readline
/usr/include/readline/chardefs.h
/usr/include/readline/history.h
/usr/include/readline/keymaps.h
/usr/include/readline/readline.h
/usr/include/readline/rlconf.h
/usr/include/readline/rlstdc.h
/usr/include/readline/rltypedefs.h
/usr/include/readline/tilde.h
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libhistory.a
/usr/lib/x86_64-linux-gnu/libreadline.a
/usr/share
/usr/share/doc
/usr/share/info
/usr/lib/x86_64-linux-gnu/libhistory.so
/usr/lib/x86_64-linux-gnu/libreadline.so
/usr/share/doc/libreadline-dev

gcc命令

$ gcc -L/usr/lib/x86_64-linux-gnu -lreadline -Wall -Werror -v -o code code.c

gcc err输出

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.3.0-27ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) 
COLLECT_GCC_OPTIONS='-L/usr/lib/x86_64-linux-gnu' '-Wall' '-Werror' '-v' '-o' 'code' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/7/cc1 -quiet -v -imultiarch x86_64-linux-gnu code.c -quiet -dumpbase code.c -mtune=generic -march=x86-64 -auxbase code -Wall -Werror -version -fstack-protector-strong -Wformat-security -o /tmp/ccLBo26t.s
GNU C11 (Ubuntu 7.3.0-27ubuntu1~18.04) version 7.3.0 (x86_64-linux-gnu)
    compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/7/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C11 (Ubuntu 7.3.0-27ubuntu1~18.04) version 7.3.0 (x86_64-linux-gnu)
    compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: c8081a99abb72bbfd9129549110a350c
COLLECT_GCC_OPTIONS='-L/usr/lib/x86_64-linux-gnu' '-Wall' '-Werror' '-v' '-o' 'code' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccqaH7jD.o /tmp/ccLBo26t.s
Die GNU-Assembler-Version 2.30 (x86_64-linux-gnu) benutzt die BFD-Version (GNU Binutils for Ubuntu) 2.30
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-L/usr/lib/x86_64-linux-gnu' '-Wall' '-Werror' '-v' '-o' 'code' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/7/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/7/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper -plugin-opt=-fresolution=/tmp/ccYuIgyM.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o code /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/7 -L/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/7/../../.. -lreadline /tmp/ccqaH7jD.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
/tmp/ccqaH7jD.o: In Funktion »main«:
code.c:(.text+0x10): Warnung: undefinierter Verweis auf »readline«
collect2: error: ld returned 1 exit status

我尝试了Internet上提供的几种解决方案,例如以下链接,但没有一个起作用:

How do I properly reference the GNU readline library to scan terminal input?

-L/usr/local/lib -I/usr/local/include 

https://www.linuxquestions.org/questions/programming-9/undefined-reference-to-readline-494533/

#include "/usr/include/readline/readline.h"

有人有想法吗?

(顺便说一句,可能并不重要:readline在我的Mac上可用,但在我的Ubuntu虚拟机中不可用)

1 个答案:

答案 0 :(得分:4)

gcc .... code.c .... -lreadline

来自gcc link options

  

-l库
  -llibrary
  ...
  在命令中写入此选项的位置会有所不同。链接器按照指定的顺序搜索和处理库和目标文件。因此,“ foo.o -lz bar.o”在文件foo.o之后但在bar.o之前搜索库“ z”。如果bar.o引用了“ z”中的函数,则可能不会加载这些函数。