cgcccompiler-errorsreadline

error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration]


I'm trying to implement rl_replace_line() in my code but when I try to compile it like that:

gcc -lreadline test.c -o test

I get this error message:

error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

however I think I used the good header files? here is my code:

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

I don't understand why it doesn't work, I hope you guys can help thanks!


Solution

  • I managed to solve my problem by including the correct path with:

    -L .brew/opt/readline/lib and -I .brew/opt/readline/include

    now I compile like this and it's working:

    gcc test.c -o test -lreadline -L .brew/opt/readline/lib -I .brew/opt/readline/include