EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

enabling sprintf

Started by paloalgodon August 22, 2006
From: "Steve Franks"
To: "No Reply"
Date: Tue, 22 Aug 2006 10:47:15 -0700
Subject: enabling sprintf
Hi all.

Got a makefile something like this:

LDFLAGS += -lnewlib-lpc -lc -lgcc -lnewlib-lpc -lm -lnewlib-lpc
-lstdc++ -lc

If I put a call to sprintf in my code, I get this:

`_malloc_trim_r':mallocr.c: (.text+0x48): undefined reference to `_sbrk_r'
:mallocr.c:(.text+0x70): undefined reference to `_sbrk_r'
:mallocr.c:(.text+0xb4): undefined reference to `_sbrk_r'
C:\WinARM\arm-elf\lib\libc.a(makebuf.o): In function `__smakebuf':
makebuf.c:(.text+0x44): undefined reference to `_fstat_r'
:makebuf.c:(.text+0xe4): undefined reference to `isatty'
C:\WinARM\arm-elf\lib\libc.a(mallocr.o): In function
`_malloc_r':mallocr.c:(.text+0x430): undefined reference to `_sbrk_r'
:mallocr.c:(.text+0x5ec): undefined reference to `_sbrk_r'
C:\WinARM\arm-elf\lib\libc.a(stdio.o): In function
`__sread':stdio.c:(.text+0x1c): undefined reference to `_read_r'
C:\WinARM\arm-elf\lib\libc.a(stdio.o ): In function
`__swrite':stdio.c:(.text+0x78): undefined reference to `_lseek_r'
:stdio.c:(.text+0x9c): undefined reference to `_write_r'
C:\WinARM\arm-elf\lib\libc.a(stdio.o): In function
`__sseek':stdio.c:(.text+0xc0): undefined reference to `_lseek_r'
C:\WinARM\arm-elf\lib\libc.a(stdio.o): In function
`__sclose':stdio.c:(.text+0xf8): undefined reference to `_close_r'
make: *** [main.elf] Error 1

Do newlib and libc not play nice together, or do I need to stuff in
another lib to link with?

I added -lg which got rid of similar errors for gcvt, but can't seem
to get printf to play...

Thanks,
Steve

An Engineer's Guide to the LPC2100 Series

paloalgodon wrote:

> From: "Steve Franks" > >
> To: "No Reply" > >
> Date: Tue, 22 Aug 2006 10:47:15 -0700
> Subject: enabling sprintf
> Hi all.
>
> Got a makefile something like this:
>
> LDFLAGS += -lnewlib-lpc -lc -lgcc -lnewlib-lpc -lm -lnewlib-lpc
> -lstdc++ -lc
>
> If I put a call to sprintf in my code, I get this:
>
> `_malloc_trim_ r':mallocr. c: (.text+0x48) : undefined reference to
> `_sbrk_r'
> :mallocr.c:( .text+0x70) : undefined reference to `_sbrk_r'
> :mallocr.c:( .text+0xb4) : undefined reference to `_sbrk_r'
> C:\WinARM\arm- elf\lib\libc. a(makebuf. o): In function `__smakebuf' :
> makebuf.c:(. text+0x44) : undefined reference to `_fstat_r'
> :makebuf.c:( .text+0xe4) : undefined reference to `isatty'
> C:\WinARM\arm- elf\lib\libc. a(mallocr. o): In function
> `_malloc_r': mallocr.c: (.text+0x430) : undefined reference to `_sbrk_r'
> :mallocr.c:( .text+0x5ec) : undefined reference to `_sbrk_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o) : In function
> `__sread':stdio. c:(.text+ 0x1c): undefined reference to `_read_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o ): In function
> `__swrite':stdio. c:(.text+ 0x78): undefined reference to `_lseek_r'
> :stdio.c:(.text+ 0x9c): undefined reference to `_write_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o) : In function
> `__sseek':stdio. c:(.text+ 0xc0): undefined reference to `_lseek_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o) : In function
> `__sclose':stdio. c:(.text+ 0xf8): undefined reference to `_close_r'
> make: *** [main.elf] Error 1
>
> Do newlib and libc not play nice together, or do I need to stuff in
> another lib to link with?
>
Yessir, you do. It will be the floating point library "libm", to get
that you will have to build newlib yourself. Not many embedded people
want huge binaries being produced just so that they have the convenience
of printf(). Usually you can use functions like itoa() or ecvt(), or
write your own printd() function (ala' Kerningham & Ritchie Blue Book).
> I added -lg which got rid of similar errors for gcvt, but can't seem
> to get printf to play...
>

Again, that is the floating point precision library that printf() needs
to have linked in.
TomW

--
Tom Walsh - WN3L - Embedded Systems Consultant
http://openhardware.net http://cyberiansoftware.com http://openzipit.org
"Windows? No thanks, I have work to do..."
----------------
paloalgodon wrote:

> From: "Steve Franks" > >
> To: "No Reply" > >
> Date: Tue, 22 Aug 2006 10:47:15 -0700
> Subject: enabling sprintf
> Hi all.
>
> Got a makefile something like this:
>
> LDFLAGS += -lnewlib-lpc -lc -lgcc -lnewlib-lpc -lm -lnewlib-lpc
> -lstdc++ -lc
>
> If I put a call to sprintf in my code, I get this:
>
> `_malloc_trim_ r':mallocr. c: (.text+0x48) : undefined reference to
> `_sbrk_r'
> :mallocr.c:( .text+0x70) : undefined reference to `_sbrk_r'
> :mallocr.c:( .text+0xb4) : undefined reference to `_sbrk_r'
> C:\WinARM\arm- elf\lib\libc. a(makebuf. o): In function `__smakebuf' :
> makebuf.c:(. text+0x44) : undefined reference to `_fstat_r'
> :makebuf.c:( .text+0xe4) : undefined reference to `isatty'
> C:\WinARM\arm- elf\lib\libc. a(mallocr. o): In function
> `_malloc_r': mallocr.c: (.text+0x430) : undefined reference to `_sbrk_r'
> :mallocr.c:( .text+0x5ec) : undefined reference to `_sbrk_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o) : In function
> `__sread':stdio. c:(.text+ 0x1c): undefined reference to `_read_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o ): In function
> `__swrite':stdio. c:(.text+ 0x78): undefined reference to `_lseek_r'
> :stdio.c:(.text+ 0x9c): undefined reference to `_write_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o) : In function
> `__sseek':stdio. c:(.text+ 0xc0): undefined reference to `_lseek_r'
> C:\WinARM\arm- elf\lib\libc. a(stdio.o) : In function
> `__sclose':stdio. c:(.text+ 0xf8): undefined reference to `_close_r'
> make: *** [main.elf] Error 1
>
Uhhh, sorry, I should have looked more closely at the linker errors. :(

Those are "stubs" that are missing. Fucntions that deal with hardware
need stub-code, which you write, to touch the hardware. IIRC, you can
turn that off when you rebuild newlib.

TomW

> Do newlib and libc not play nice together, or do I need to stuff in
> another lib to link with?
>
> I added -lg which got rid of similar errors for gcvt, but can't seem
> to get printf to play...
>
> Thanks,
> Steve
>
>

--
Tom Walsh - WN3L - Embedded Systems Consultant
http://openhardware.net http://cyberiansoftware.com http://openzipit.org
"Windows? No thanks, I have work to do..."
----------------
At 06:38 PM 8/22/2006 -0400, Tom Walsh wrote:
>paloalgodon wrote:
>
> > From: "Steve Franks" > > >
> > To: "No Reply" > > >
> > Date: Tue, 22 Aug 2006 10:47:15 -0700
> > Subject: enabling sprintf
> > Hi all.
> >
> > Got a makefile something like this:
> >
> > LDFLAGS += -lnewlib-lpc -lc -lgcc -lnewlib-lpc -lm -lnewlib-lpc
> > -lstdc++ -lc
> >
> > If I put a call to sprintf in my code, I get this:
> >
> > `_malloc_trim_ r':mallocr. c: (.text+0x48) : undefined reference to
> > `_sbrk_r'
> > :mallocr.c:( .text+0x70) : undefined reference to `_sbrk_r'
>Uhhh, sorry, I should have looked more closely at the linker errors. :(
>
>Those are "stubs" that are missing. Fucntions that deal with hardware
>need stub-code, which you write, to touch the hardware. IIRC, you can
>turn that off when you rebuild newlib.

He's linking in newlib-lpc which provides the stubs. I suspect it's more
likely a link order problem.

To the OP, can you provide your link line?

Robert
Another sign of the end of civilization, our technical magazines are
getting chatty
From an EETimes product descriptions 2006/08/09
".... systems that can sample gobs of inputs simultaneously"
Now just what is the technical definition for gobs again?
http://www.aeolusdevelopment.com/
>He's linking in newlib-lpc which provides the stubs. I suspect it's
>more likely a link order problem.

>To the OP, can you provide your link line?

I did, no?

> > LDFLAGS += -lnewlib-lpc -lc -lgcc -lnewlib-lpc -lm -lnewlib-lpc
> > -lstdc++ -lc

Or did you want the whole thing:

LIB_ARCH = C:\WinARM\arm-elf\lib
LIB_ARCH2 = C:\WinARM\lib\gcc\arm-elf
LIB_LOCAL = C:\projects\lib
vpath %.a $(LIB_ARCH);$(LIB_ARCH2);$(LIB_LOCAL)
LIBFLAGS := -L$(LIB_ARCH) -L$(LIB_ARCH2) -L$(LIB_LOCAL)
LDFLAGS = -nostartfiles -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(LIBFLAGS)
LDFLAGS += -lnosys -lg -lnewlib-lpc -lg -lc -lgcc -lnewlib-lpc -lm
-lnewlib-lpc -lstdc++ -lsupc++ -lg -lnosys
ifeq ($(RUN_MODE),RAM_RUN)
LDFLAGS +=-Tbuild/$(MCU)-RAM.ld
else
LDFLAGS +=-Tbuild/$(MCU)-ROM.ld
endif
$(TARGET).elf: $(AOBJ) $(SOURCES:.c=.o) $(CPPSOURCES:.cpp=.o)
$(CXX) $(CFLAGS) $^ --verbose --output $@ $(LDFLAGS)
Also, as a warning, I forgot to mention arm-elf-g++.exe is running
over this, not arm-elf-gcc. I wrapped all the includes in an extern
"C" but it did not rectify the issue.

Thanks,
Steve
At 08:47 PM 8/23/2006 +0000, paloalgodon wrote:

> >He's linking in newlib-lpc which provides the stubs. I suspect it's
> >more likely a link order problem.
>
> >To the OP, can you provide your link line?
>
>I did, no?

no, just a makefile variable.

> > > LDFLAGS += -lnewlib-lpc -lc -lgcc -lnewlib-lpc -lm -lnewlib-lpc
> > > -lstdc++ -lc
>
>Or did you want the whole thing:
>
>LIB_ARCH = C:\WinARM\arm-elf\lib
>LIB_ARCH2 = C:\WinARM\lib\gcc\arm-elf
>LIB_LOCAL = C:\projects\lib
>vpath %.a $(LIB_ARCH);$(LIB_ARCH2);$(LIB_LOCAL)
>LIBFLAGS := -L$(LIB_ARCH) -L$(LIB_ARCH2) -L$(LIB_LOCAL)
>LDFLAGS = -nostartfiles -Wl,-Map=$(TARGET).map,--cref
>LDFLAGS += $(LIBFLAGS)
>LDFLAGS += -lnosys -lg -lnewlib-lpc -lg -lc -lgcc -lnewlib-lpc -lm
>-lnewlib-lpc -lstdc++ -lsupc++ -lg -lnosys
>ifeq ($(RUN_MODE),RAM_RUN)
>LDFLAGS +=-Tbuild/$(MCU)-RAM.ld
>else
>LDFLAGS +=-Tbuild/$(MCU)-ROM.ld
>endif
>$(TARGET).elf: $(AOBJ) $(SOURCES:.c=.o) $(CPPSOURCES:.cpp=.o)
> $(CXX) $(CFLAGS) $^ --verbose --output $@ $(LDFLAGS)

Hmm, that looks like it should work. Your error indicates that you have a
copy of newlib linking in after newlib-lpc. With the verbose flag you
should have a list if the input to ld which should give more of a hint as
to what's happening.

Robert

http://www.aeolusdevelopment.com/

From the Divided by a Common Language File (Edited to protect the guilty)
ME - "I'd like to get Price and delivery for connector Part # XXXXX"
Dist./Rep - "$X.XX Lead time 37 days"
ME - "Anything we can do about lead time? 37 days seems a bit high."
Dist./Rep - "that is the lead time given because our stock is live.... we
currently have stock."

The 2024 Embedded Online Conference