Can't get sprintf to work in standalone program
Hi,
I'm trying to use the sprintf function in a C program written for U-boot. As far as I understand, to get the function I have to add it to the exports.h, _exports.h and exports.c file as per the README.Standalone file in /doc (the function is declared in common.h). Now if I do this, the program compiles fine, and all is dandy, until I actually start using the function. It generates garbage. printf and alike works fine, and the program runs like it should, except the places where sprintf is used, I get garbage (meaning some environment variables are looking pretty nasty since the result of the sprintf functions are used for these).
When compiling I link to the examples/libstubs.a file and lib_generic/libgeneric.a where the code I'm using should be present. Can anyone give me a clue to what the problem could be or if I misunderstood the concept completely?
I searched the lists and in my frustration I tried to use some code from an earlier post where some guy had problems with sprintf. Here Wolfgang Denk wrote a small piece of code that used the function, and this was what I then put in my own code, just to see if I could get *anything* working with sprintf. The code I used was this:
...
unsigned char buf[128];
unsigned char a[6] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, }; int i;
...
printf ("Before: "); for (i=0; i<6; ++i) printf (" %02X",a[i]); putc ('\n');
sprintf (buf, "%02X:%02X:%02X:%02X:%02X:%02X", a[0], a[1], a[2], a[3], a[4], a[5]);
printf ("After: "); for (i=0; i<6; ++i) printf (" %02X",a[i]); putc ('\n');
printf ("buf=\"%s\"\n", buf);
...
When I do this, the line that defines the a[6] thing generates a compiler error stating an undefined reference to memcpy. Since this error is present every time I try to initialize a string (char*) or char array I'm guessing this might be the problem, but I'm not sure...
The platform is ARM (AT91RM9200)... U-boot version is 2009.06.
I'm new to standalone programming for U-boot so this might surely be the problem, so any help is appreciated :)
Kind regards,
Martin Hejnfelt
Dear Martin Hejnfelt,
Quoted 13 lines · In message <1247062359.4070.21.camel@hejnfelt-2> you wrote:
I'm trying to use the sprintf function in a C program written for U-boot. As far as I understand, to get the function I have to add it to the
exports.h,_exports.handexports.cfile as per the README.Standalone file in /doc (the function is declared incommon.h). Now if I do this, the program compiles fine, and all is dandy, until I actually start using the function. It generates garbage. printf and alike works fine, and the program runs like it should, except the places where sprintf is used, I get garbage (meaning some environment variables are looking pretty nasty since the result of the sprintf functions are used for these).
When compiling I link to the examples/libstubs.a file and lib_generic/libgeneric.a where the code I'm using should be present.
Note that 1) linking libgeneric.a should not be needed and 2) doing so makes your standalone application to be GPLed.
Quoted 4 lines
When I do this, the line that defines the a[6] thing generates a compiler error stating an undefined reference to memcpy. Since this error is present every time I try to initialize a string (char*) or char array I'm guessing this might be the problem, but I'm not sure...
You probably have to come up with a linker script and a concept of where your data segment is located...
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The price of curiosity is a terminal experience.
- Terry Pratchett, _The Dark Side of the Sun_