I have following problem - I've done baking pi course without any problems and then I 've decided to link this pure asm with some C. And I have done this and everything is working nice as far as I decided to use function sprintf to convert int to char*. Now when im trying to compile im enjoying following error
ctesty.c:(.text+0x20): undefined reference to 'sprintf'
make: *** [build/output.elf] Error 1
of course i have included stdio.h, i also tried to include this lib directly in makefile but without success.
You want to link with
libc.so
. Try adding-lc
in the link step in your makefiles.Update after Better Understanding of the Question
The Baking Pi examples build a
kernel.img
that runs in place of the usual Linux kernel. This means that the C standard library is not available. You'll need to supply your ownsprintf()
implementation, e.g.:uart_send_char()
ee_printf()
intosprintf()
by makingbuf
a function parameterWhy a Userspace libc Wouldn't Work Without Source Modifications in any Kernel
Conceptually:
malloc
,free
), the file system (fopen
,fclose
and all of stdio), environment variables (getenv
), error handling facilities likesetjmp
longjmp
and more.malloc
in a program running under an operating system?brk
,sbrk
andmmap
under Linux.brk
,mmap
etc wouldn't be the right interfaces to allocate memory.malloc
like facilities, they won't be able to use the userspacemalloc
implementations unmodified.fopen
,getenv
are not really that useful. Most kernel writers choose not to include them.In a more mechanical sense:
$(ARMGNU)-ld --no-undefined $(OBJECTS) -Map $(MAP) -o $(BUILD)output.elf -T $(LINKER)
. This doesn't linkbuild/output.elf
with any libc implementation so the linker wouldn't be able to find a symbol namedsprintf
.gcc T.c -o T
, gcc implicitly links in libc unless an option like-nostdlib
is passed.But the conceptually important points are:
There are C standard library implementations targeting bare metal environments. See for example, newlib.