代码之家  ›  专栏  ›  技术社区  ›  phip1611

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

  •  1
  • phip1611  · 技术社区  · 6 年前

    我知道在Stackoverflow上有很多关于这个的帖子,但是没有一个解决方案对我有帮助。我将Ubuntu 18.04用于开发人员通常需要的所有内容: build-essential, libreadline-dev, ...

    /usr/include/readline 但是readline肯定在那里!

    对“readline”的未定义引用


    编辑

    $ gcc -lreadline -o code code.c
    

    在MacOS上工作 使用apple gcc(使用clang)以及gnu gcc(通过brew安装)。只有在Ubuntu上,链接器标志必须在“code.c”之后

    (可能是因为Ubuntu使用gcc-7,而在我的Mac电脑上我使用的是gcc-8)


    代码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开发包信息

    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错误输出

    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
    

    我尝试了互联网上提供的几种解决方案,如以下链接,但都不起作用:

    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"
    

    有人有主意吗?

    1 回复  |  直到 6 年前
        1
  •  4
  •   KamilCuk    6 年前
    gcc .... code.c .... -lreadline
    

    从…起 gcc link options

    -图书馆
    -图书馆
    ...
    在命令中写入此选项的位置会有所不同;链接器按照指定的顺序搜索和处理库和对象文件。因此,foo.o-lzbar.o在文件foo.o之后、bar.o之前搜索库z。如果bar.o引用z中的函数,则可能不会加载这些函数。