Undefined link to `readline '

I had a problem trying to run a sample GNU Readline library library available on wikipedia . Here it is:

#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <readline/readline.h> #include <readline/history.h> int main() { char* input, shell_prompt[100]; // Configure readline to auto-complete paths when the tab key is hit. rl_bind_key('\t', rl_complete); for(;;) { // Create prompt string from user name and current working directory. snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024)); // Display prompt and read input (nb input must be freed after use)... input = readline(shell_prompt); // Check for EOF. if (!input) break; // Add input to history. add_history(input); // Do stuff... // Free input. free(input); } } 

I am working on a limited environment because readline is not available, so I had to download the sources, compile them and install them in my home directory.

This is the structure inside my home directory:

 .local โ”œโ”€โ”€ include โ”‚  โ””โ”€โ”€ readline โ”‚    โ”œโ”€โ”€ chardefs.h โ”‚    โ”œโ”€โ”€ history.h โ”‚    โ”œโ”€โ”€ keymaps.h โ”‚    โ”œโ”€โ”€ readline.h โ”‚    โ”œโ”€โ”€ rlconf.h โ”‚    โ”œโ”€โ”€ rlstdc.h โ”‚    โ”œโ”€โ”€ rltypedefs.h โ”‚    โ””โ”€โ”€ tilde.h โ””โ”€โ”€ lib  โ”œโ”€โ”€ libhistory.a  โ”œโ”€โ”€ libhistory.so -> libhistory.so.6  โ”œโ”€โ”€ libhistory.so.6 -> libhistory.so.6.2  โ”œโ”€โ”€ libhistory.so.6.2 โ”œโ”€โ”€ libreadline.a  โ”œโ”€โ”€ libreadline.so -> libreadline.so.6  โ”œโ”€โ”€ libreadline.so.6 -> libreadline.so.6.2  โ””โ”€โ”€ libreadline.so.6.2 

The problem is that when I call gcc, it causes me an error:

 $ gcc hello.c -o hello_c.o -I /home/my-home-dir/.local/include /tmp/cckC236E.o: In function `main': hello.c:(.text+0xa): undefined reference to `rl_complete' hello.c:(.text+0x14): undefined reference to `rl_bind_key' hello.c:(.text+0x60): undefined reference to `readline' hello.c:(.text+0x77): undefined reference to `add_history' collect2: error: ld returned 1 exit status 

There is an answer about this here , but I'm not using Netbeans, and I'm not quite sure how to specify the library path using the line command.

I tried to tell the linker where the libraries are, but the result is still the same:

 $ gcc hello.c -o hello_c.o -I /home/my-home-dir/.local/include -Xlinker "-L /home/my-home-dir/.local/lib" /tmp/cceiePMr.o: In function `main': hello.c:(.text+0xa): undefined reference to `rl_complete' hello.c:(.text+0x14): undefined reference to `rl_bind_key' hello.c:(.text+0x60): undefined reference to `readline' hello.c:(.text+0x77): undefined reference to `add_history' collect2: error: ld returned 1 exit status 

Any ideas on what I'm missing here?

+7
source share
1 answer

You need to link the actual library again with -lreadline in gcc arguments

+12
source

All Articles