2

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!

luweglarz
  • 103
  • 2
  • 9

2 Answers2

1

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
luweglarz
  • 103
  • 2
  • 9
  • 1
    Note this is because brew refuses to link readline. Warning: Refusing to link macOS provided/shadowed software: readline For compilers to find readline you may need to set: export LDFLAGS="-L/usr/local/opt/readline/lib" export CPPFLAGS="-I/usr/local/opt/readline/include" – Chris Feb 03 '22 at 00:20
1

I solve the issue by finding the libreadline.a with the command ==> find / -name libreadline.a 2> /dev/null.

The result is ==> /opt/homebrew/Cellar/readline/8.1.2/lib/libreadline.a

So I put the correct path for the compilation: -L libs -lft -L /opt/homebrew/Cellar/readline/8.1.2/lib -lreadline.

No this is working perfectly

Vess
  • 11
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 28 '22 at 18:51