Where is the error (stdout)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Where is the error (stdout)

Royce Pereira
Hi,

I am using Avr-gcc-9.1.0

Here's some code (just cut paste from AVR-lib):

//      .......
#include <stdio.h>
//      ......

void uart_putchar(char c, FILE *stream)
{
 if (c == '\n')
uart_putchar('\r', stream);
 loop_until_bit_is_set(UCSRA, UDRE);
 UDR = c;
 return ;
}
//===================================
//set stream pointer
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
//====================================

I am getting this warning:

In file included from PkLoad-6D-Ser-72x72x65-1214.c:12:
PkLoad-6D-Ser-72x72x65-1214.c:86:42: warning: initialization of 'int (*)(char,  struct __file *)' from incompatible pointer type 'void (*)(char,  FILE *)' {aka 'void (*)(char,  struct __file *)'} [-Wincompatible-pointer-types]

The code compiles (but for this warning) and works too, on hardware.

This code worked fine in older version of AVR-GCC (4.6 and earlier)

I ignored this warning for a long time. 
Recently, I  got curious as to the nature of the error.

So what has changed recently ? Thanks!

--
Best Regards,

-- Royce Pereira
Col
Reply | Threaded
Open this post in threaded view
|

Re: Where is the error (stdout)

Col

>
> void uart_putchar(char c, FILE *stream)
> {
>  if (c == '\n')
> uart_putchar('\r', stream);
>  loop_until_bit_is_set(UCSRA, UDRE);
>  UDR = c;
>  return ;
> }
>
I suspect it's because your returning a void instead of an int,

Here is some code that I used to test stdio on avrlibc ( atmega128 )

which compiles fine with gcc 5.4.0


static int uart_putchar(char c, FILE *stream)
{
     if (c == '\n')
         uart_putchar('\r', stream);
     loop_until_bit_is_set(UCSR0A, UDRE);
     UDR0 = c;
     return 0;
}




Cheers

Colin



Reply | Threaded
Open this post in threaded view
|

Re: Where is the error (stdout)

David Kelly

On Feb 7, 2020, at 5:58 PM, Col <[hidden email]> wrote:

>> void uart_putchar(char c, FILE *stream)
>> {
>>  if (c == '\n')
>> uart_putchar('\r', stream);
>>  loop_until_bit_is_set(UCSRA, UDRE);
>>  UDR = c;
>>  return ;
>> }
>
> I suspect it's because your returning a void instead of an int,

In Unix convention its

        int putc( int, FILE* )
        int putchar( int )

Where upon success one returns the character put or -1 or EOF for failure. If replacing standard function one should return the expected value else something will break.

--
David Kelly N4HHE, [hidden email]
============================================================
Whom computers would destroy, they must first drive mad.