E.Z*_*.Z. 9 c readline libreadline
我在尝试运行维基百科中提供的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在创建一个受限制的环境,因为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
Run Code Online (Sandbox Code Playgroud)
问题是,当我调用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
Run Code Online (Sandbox Code Playgroud)
有关于这个答案在这里,但我不使用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
Run Code Online (Sandbox Code Playgroud)
我在这里可能缺少什么想法?