undefined reference to `readline' [duplicate]

我的梦境 提交于 2019-12-03 18:01:57

问题


I'm having a problem trying to run the GNU Readline library sample code available in wikipedia. Here it goes:

#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 (n.b. 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'm working on a restricted environment were readline was not available, so I had to download the sources, compile it and install it to my home dir.

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, when I call gcc it throws 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 path to the library on the command line.

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 what I might be missing here?


回答1:


You need to link againts the actual library using -lreadline in gcc arguments



来源:https://stackoverflow.com/questions/14773420/undefined-reference-to-readline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!