0

I've a problem when I try to compile my program with my static library. I create the object file of my .c files whith gcc -c ft_putstr.c. Then I execute ar -rcs libft.a ft_putstr.o and then I make gcc main.c -L. -lft and I've

warning: implicit declaration of function 'ft_putstr' is invalid in C99.

The binary is created but I don't want this warning even if it's work like that. It works if I had the flag -std="c89" on GCC but I have tu use C99.

This is my main :

int main(void)
{
     ft_putstr("Bonjour");
     return (0);
}

This my ft_putstr.c :

#include <unistd.h>

    void    ft_putstr(char *str) 
    {
         (*str) ? write(1, str, 1), ft_putstr(str + 1) : 0; 
    }
jiten
  • 5,128
  • 4
  • 44
  • 73
sebasthug
  • 81
  • 1
  • 7
  • Do you import `ft_putstr.h` from `main.c`? Does that header define the function? – sapi Aug 11 '14 at 09:52
  • No, I don't import it because I've seen a video (given by my school) which make work the program without any .h. I've already tried with a .h and yes it works. – sebasthug Aug 11 '14 at 09:54
  • The problem is that `main()` cannot see it. Put it in a header and include it in the file with `main()` If you can't import a header, then just declare it static extern in one file and include the prototype in the file with `main` – David C. Rankin Aug 11 '14 at 09:54
  • Without declaring `ft_putstr`, you're always going to get an implicit declaration warning. Hell, you could declare it in main.c if you want (please don't though; that'd be stupid). – sapi Aug 11 '14 at 09:55
  • I think than you have a very higher level than the mine so I will add a header file, this is the better option I think now. Many thanks for your help and sorry if I was unpleasant. It does more than 2 hours than I'm on it. – sebasthug Aug 11 '14 at 09:59
  • @DavidC.Rankin what is "static extern"? – Jonathan Wakely Aug 11 '14 at 10:03
  • You will need to declare the function in one file completely. In your case it is not declared in the file with main. Then in the file with main add a `extern` declaration. (i.e. `extern void ft_putstr(char *str);` – David C. Rankin Aug 11 '14 at 10:07
  • @sapi declaring it in main.c is slightly better than not declaring it at all – M.M Aug 11 '14 at 11:29

1 Answers1

5

You should include a header file which has a declaration like

void    ft_putstr(char *str);

or you can insert this line in your main.c

extern void    ft_putstr(char *str);
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • `extern` is superfluous here, all function declarations are extern unless you specifically indicate `static`. – M.M Aug 11 '14 at 11:30