未定义的引用'的ReadLine' [英] undefined reference to `readline'

查看:723
本文介绍了未定义的引用'的ReadLine'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,试图运行 GNU的Readline 库示例$ C $ç href=\"https://en.wikipedia.org/wiki/GNU_readline#Sample_$c$c\">可用。这里有云:

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);
    }
}

我工作的一个受限制的环境中的readline不可用,所以我不得不下载源代码,编译并安装到我的主目录。

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

问题是,当我叫GCC它将引发我一个错误:

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

有大约这里此一个答案,但我不使用NetBeans和我M不太清楚如何指定要在命令行上库的路径。

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?

推荐答案

您需要在反对票GCC参数使用-lreadline实际库链接

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

这篇关于未定义的引用'的ReadLine'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆