问题描述
我正在尝试在我的代码中实现 rl_replace_line() 但是当我尝试像这样编译它时:
gcc -lreadline test.c -o test
我收到此错误消息:
error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
# include <stdio.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <unistd.h>
# include <stdlib.h>
char *get_line()
{
char *line;
line = NULL;
if (line)
{
free(line);
line = NULL;
}
line = readline("Minishell>");
if (line)
add_history(line);
return (line);
}
void sig_handler(int signum)
{
if (signum == SIGINT)
{
printf("\n");
rl_on_new_line();
rl_replace_line("",0);
rl_redisplay();
}
}
int main(void)
{
char *line;
signal(SIGINT,sig_handler);
line = get_line();
printf("%s\n",line);
}
我不明白为什么它不起作用,希望你们能帮忙谢谢!
解决方法
我设法通过包含正确的路径来解决我的问题:
-L .brew/opt/readline/lib
和 -I .brew/opt/readline/include
现在我像这样编译并且它正在工作:
gcc test.c -o test -lreadline -L .brew/opt/readline/lib -I .brew/opt/readline/include