title: undefined reference to "readline" in c file on ubuntu 18.04 (libreadline-dev installed, linked with "-lreadline"
body: <p>I'm aware that there are many threads about this on Stackoverflow but non of the solutions helped me. I'm using Ubuntu 18.04 with all the stuff that a dev typically needs: <code>build-essential, libreadline-dev, ...</code></p>
  
  <p>I had a look in <code>/usr/include/readline</code> but readline is definitely there!
  gcc always tells me:</p>
  
  <blockquote>
    <p>undefined reference to "readline"</p>
  </blockquote>
  
  <p>This happens by the way independently if i'm linking with "-lreadline" or not.</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>As user @Kamil Cuk already provided the simple solution I'd like to add some more information:</p>
  
  <pre><code>$ gcc -lreadline -o code code.c
  </code></pre>
  
  <p><strong>works on MacOS</strong> with apple-gcc (which uses clang) as well as with gnu-gcc (installed via brew). Only on Ubuntu the linker-flag has to be after "code.c"</p>
  
  <p>(perhaps because Ubuntu uses gcc-7 and on my Mac I'm using gcc-8)</p>
  
  <p><em>Edit end</em></p>
  
  <hr>
  
  <p><strong>code.c</strong></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;readline/readline.h&gt;
  
  int main(void) {
      char * buffer = readline("enter sth: ");
      printf("%s\n", buffer);
      printf("bye!\n");
  }
  </code></pre>
  
  <p><strong>libreadline-dev package info</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>gcc command</strong></p>
  
  <pre><code>$ gcc -L/usr/lib/x86_64-linux-gnu -lreadline -Wall -Werror -v -o code code.c
  </code></pre>
  
  <p><strong>gcc err output</strong></p>
  
  <pre><code>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 &lt;...&gt; 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
  </code></pre>
  
  <hr>
  
  <p>I tried several solutions provided on the internet, such as the following links but none of them worked:</p>
  
  <p><a href="https://stackoverflow.com/questions/3783149/how-do-i-properly-reference-the-gnu-readline-library-to-scan-terminal-input">How do I properly reference the GNU readline library to scan terminal input?</a></p>
  
  <pre><code>-L/usr/local/lib -I/usr/local/include 
  </code></pre>
  
  <hr>
  
  <p><a href="https://www.linuxquestions.org/questions/programming-9/undefined-reference-to-readline-494533/" rel="nofollow noreferrer">https://www.linuxquestions.org/questions/programming-9/undefined-reference-to-readline-494533/</a></p>
  
  <pre><code>#include "/usr/include/readline/readline.h"
  </code></pre>
  
  <hr>
  
  <p>does anyone have an idea? </p>
  
  <p>(by the way, probably don't important: readline works on my Mac but not in my Ubuntu Virtual Machine)</p>