13

wikipedia で入手できるGNU Readlineライブラリのサンプル コードを実行しようとすると問題が発生します。ここに行きます:

#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 が利用できない制限された環境で作業しているため、ソースをダウンロードしてコンパイルし、ホーム ディレクトリにインストールする必要がありました。

これは私のホームディレクトリ内の構造です:

.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 を呼び出すと、エラーがスローされることです。

$ 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 を使用しておらず、コマンド ラインでライブラリへのパスを指定する方法がよくわかりません。

ライブラリの場所をリンカーに伝えようとしましたが、結果は同じです。

$ 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

ここで見逃している可能性のあるアイデアはありますか?

4

1 に答える 1

29

-lreadlinein gcc 引数を使用して、実際のライブラリにリンクする必要があります。

于 2013-02-08T13:19:13.367 に答える