newlib, sscanf et al

I use the normal gcc arm toolchain and newlib under Linux.

I was going like a steam train until I wanted to use sscanf to do some number formatting.

All very straightforward stuff, eg:

#include <stdio.h>
...
sscanf(theString,"The string = %d",12345);
...

arm-elf-ld then reports “… undefined reference to `sscanf’”.

I have tried everything in the book to fix the problem.

I have checked that the linker correctly picks up the reqired libc.a, and that sscanf.o is indeed in that library.

I realise after reading Bill Gatliff’s article that using newlib is not straightforward but this is so simple! If the linker can see the correct library and it has the correct object code in it how can it not resolve the reference?

Update: If I copy sscanf.o from the newlib build directory into my project and link it there, all is well; but that’s not a very elegant fix.

Any advice / valium gratefully received!

Bob

Could you provide a log of the build, and the mapfile.

ld will by default only do one pass to resolve externals, so if your objects are after libc then you will get unresolved externals.

Are you using gcc or ld directly to link?

Cheers

Spen

ntfreak:
…ld will by default only do one pass to resolve externals, so if your objects are after libc then you will get unresolved externals…

Aaaaaarrrrrgh! I’d forgotten that Spen. A final -lc on the ld command line fixed it properly. Thank you so much for helping me.

Edit 25 minutes later: But now I have problems if I try to use sprintf:

......newlib_build/arm-elf/newlib/libc/libc.a(sprintf.o): In function `_sprintf_r':
......newlib_sources/newlib/libc/stdio/sprintf.c:346: undefined reference to `_vfprintf_r'

1 step forward, 2 back!

Grateful Bob.

Why do you not just use gcc to link, you never have any problems with libs.

You can force ld to keep checking for externals by using, this is roughly what the gcc driver does.

–start-group archives --end-group

Cheers

Spen

ntfreak:
Why do you not just use gcc to link, you never have any problems with libs.

You can force ld to keep checking for externals by using, this is roughly what the gcc driver does.

–start-group archives --end-group

Cheers

Spen

Thanks Spen,

I agree, there’s really no reason not to use gcc - I’m just lazy and keep the makefiles from Jim Lynch’s tutorial which I know and love.

I sorted all my problems out in the end by remaking newlib.

For posterity: I used Tom Walsh’s build scripts which are ideal to get going with quickly. Unfortunately for me, the script to build newlib ran a patch on the makefile produced by configure and added a few “antibloat” flags to produce an integer only library. Very sensible for embedded stuff obviously but not for me who wants to use floating point and printf etc. during development.

Regards,

Bob