Problems printing floats

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All,

In my GCC program:
fprintf_P(stdout,PSTR("Foo=%f\n"),5.0);

produces:
Foo=?

while:
fprintf_P(stdout,PSTR("Foo=%d\n"),5);

produces:
Foo=5

I am linking in:
libprintf_flt.a
libscanf_flt.a

Any idea why the float isn't recognized?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would guess that you are not actually linking the libprintf_flt.a

Copy and paste the Studio Build Window for a complete compilation. ( Make clean first )

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make sure your makefile PRINTF_LIB value referst to the right library:

#Additional libraries.

# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min

# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt

PRINTF_LIB = $(PRINTF_LIB_FLOAT)

# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min

# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avr-gcc.exe -mmcu=atmega168 -Wl,-Map=CushionController.map CushionController.o uart.o -lprintf_flt -lscanf_flt -o CushionController.elf

So, the correct libraries are linked in. The resulting elf file is about the size you'd expect with both of those libraries linked in.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you use Mfile this would have been MUCH easier but the point is there are TWO things you must do to enable printf_flt support. The -l you show above is one half of the jigsaw the other, as show in an Mfile generated Makefile such as the one fizgig posted above is the need for:

-Wl,-u,vfprintf

So I guess your linker invocation should be:

avr-gcc.exe -mmcu=atmega168 -Wl,-Map=CushionController.map -Wl,-u,vfprintf CushionController.o uart.o -lprintf_flt -lscanf_flt -o CushionController.elf

But do yourself a favour and check out Mfile!

Oh and if you are using scanf as well you need the equivalent linker command for it too as shown in fizgig's post above.