0

I am trying to run this code but I still have an issue with this function:

# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <limits.h>
# include <errno.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <signal.h>

void sig_handler()
{
    printf ("\n");
    rl_on_new_line();
    rl_replace_line("", 0);
    rl_redisplay();
}
int main()
{
    signal(SIGQUIT, SIG_IGN);
    signal(SIGINT, sig_handler);
    while (1)
    {
        str = readline(BBLU"shell$ "BYEL);
    }
} 

I tried to install readline-8.0 using hombrew and while I'm doing this:

gcc -Wall -Werror -Wextra -lreadline ./readline-8.0/shlib/libreadline.8.0.dylib main.c

I still cannot compile the program.

I tried to do

gcc -Wall -Werror -Wextra main.c -lreadline -L ./readline-8.0/shlib -l ./readline-8.0/

and

gcc -Wall -Werror -Wextra -lreadline -L ./readline-8.0/shlib -l ./readline-8.0/ main.c No difference.

This is the result:

main.c:50:2: error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration] rl_replace_line("", 0); ^ 1 error generated.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
Abrar
  • 3
  • 5
  • Does this answer your question? [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – kaylum Apr 01 '22 at 20:52
  • You need to put the libs _after_ the objects/source that uses it. See the duplicate post for details. So try: `gcc -Wall -Werror -Wextra main.c -lreadline`. If that still doesn't solve your problem please update the post to show the _exact and complete_ error messages. – kaylum Apr 01 '22 at 20:53
  • @kaylum nope, I already tried to use -L and -l with the path of the deadline library that I have installed. But still .. it's not working – Abrar Apr 01 '22 at 21:26
  • But did you move it to the end of the command? Please show the exact command you tried and the full exact error message. – kaylum Apr 01 '22 at 21:26
  • @kaylum I tried putting the link after my source file still no difference – Abrar Apr 01 '22 at 21:27
  • gcc -Wall -Werror -Wextra -lreadline -L ./readline-8.0/shlib -l ./readline-8.0/ main.c main.c:50:2: error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration] rl_replace_line("", 0); ^ 1 error generated. – Abrar Apr 01 '22 at 21:40
  • gcc -Wall -Werror -Wextra main.c -lreadline -L ./readline-8.0/shlib -l ./readline-8.0/ I tried this also – Abrar Apr 01 '22 at 21:41
  • 1
    Please [edit](https://stackoverflow.com/posts/71712499/edit) the post to add the info. Not in comments where it can't be formatted and can be easily missed. The error is not what I thought you meant which is why it is important to give exact info. – kaylum Apr 01 '22 at 21:42
  • okay I fixed my post now – Abrar Apr 02 '22 at 00:12
  • Is that really your exact code? The error message is pointing at line 50 but there is clearly not 50 lines of code shown in your post. – kaylum Apr 02 '22 at 03:33
  • Since compiler is complaining of `implicit declaration of function`, you should check if you have properly included `readline` headers, or if the called function is declared in the headers. Also, check if this helps. https://stackoverflow.com/questions/68003253/error-implicit-declaration-of-function-rl-replace-line-is-invalid-in-c99-we – pmatkov Apr 02 '22 at 17:30
  • @kaylum well yeah I just removed the comments to make it more readable ... – Abrar Apr 03 '22 at 00:04
  • @pmatkov I already checked that and as I said in my post I tried to install the library again but still .. I can't compile it – Abrar Apr 03 '22 at 00:06
  • Ok, did you check the preprocessed file with `gcc -E` option? The compiler wouldn't be complaining for nothing. – pmatkov Apr 03 '22 at 09:12

0 Answers0