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?