Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

unix

gnu

readline

I am attempting to compile C code that utilizes the following within GNU readline.

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

I've tried changing the <> to "" and compiling both with and without the -lreadline options. Nothing seems to work. When compiling without -lreadline under gcc results in the following being generated while compiling (verbose):

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

This is the output when compiling without the -lreadline option.

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

I have been unable to resolve this error up to this point. What am I missing in either my code or while invoking gcc?

like image 322
XBigTK13X Avatar asked Sep 23 '10 23:09

XBigTK13X


People also ask

How does GNU readline work?

It allows users to move the text cursor, search the command history, control a kill ring (a more flexible version of a copy/paste clipboard) and use tab completion on a text terminal. As a cross-platform library, readline allows applications on various systems to exhibit identical line-editing behavior.

What is readline () in C?

The function readline () prints a prompt and then reads and returns a single line of text from the user. The line readline returns is allocated with malloc () ; you should free () the line when you are done with it. The declaration for readline in ANSI C is. char *readline (char * prompt );

What is libreadline7?

package: libreadline Name: libreadline Version: 7.0-1 Description: The Readline library provides a set of functions for use by applications\\ that allow users to edit command lines as they are typed in. Both Emacs\\ and vi editing modes are available.

What is readline Ubuntu?

DESCRIPTION. readline will read a line from the terminal and return it, using prompt as a prompt.


2 Answers

While compiling under UNIX I found the following is necessary to properly reference the GNU readline library:

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

This ensures that the compiler finds the readline directories and files during compilation and linking.

like image 79
XBigTK13X Avatar answered Sep 28 '22 13:09

XBigTK13X


It sounds like you don't have the libreadline development libraries installed. On Debian (including the FreeBSD port) the library is located in /lib and the development libraries (that you would link to) are in /usr/lib.

Additionally you have semi-colons after your includes, which you shouldn't have.

like image 20
Niki Yoshiuchi Avatar answered Sep 28 '22 13:09

Niki Yoshiuchi