Skip to main content
henrik
Associate
August 8, 2014
Question

Problem using sprintf

  • August 8, 2014
  • 3 replies
  • 2399 views
Posted on August 08, 2014 at 11:41

Hi community,

I am having trouble using the sprintf function to convert a variable into a string. It compiles just fine if I do

char string[11];
 sprintf(string, ''23'');

And I can see the numer 23 in ''string'' while debugging. But just using a variable instead of the number, like

int data = 23;
 sprintf(string, data);

throws these errors:

undefined reference to `__HEAP_END' SPC563Mxx OS-Less Test Application line 37, external location: \data\distribution\free-entry-toolchain-v4.6.2.1\gcc-4.6.x\libhtcos\ppc-vle\libos_sbrk.c C/C++ Problem
undefined reference to `__HEAP' SPC563Mxx OS-Less Test Application line 0, external location: c:\spc5studio\eclipse\plugins\com.st.tools.spc5.tools.hightec_1.0.0.201306281422\hightec\ppc-ht-eabi\lib\vle\libos.a(libos_sbrk.o) C/C++ Problem
undefined reference to `__HEAP' SPC563Mxx OS-Less Test Application line 42, external location: \data\distribution\free-entry-toolchain-v4.6.2.1\gcc-4.6.x\libhtcos\ppc-vle\libos_sbrk.c C/C++ Problem

I would suspect some compiler/linker issues. Does anybody have any ideas? Best regards Henrik #babycare.com
    This topic has been closed for replies.

    3 replies

    Erwan YVIN
    ST Technical Moderator
    August 8, 2014
    Posted on August 08, 2014 at 17:02

    Hello Henrik,

    it is linked to the compiler.

    the compiler does not provide an implementation of ''sbrk''.

    if you copy-paste this following code at the top of main.c

    it should compile.

    #include <errno.h>

    #include ''stdio.h''

    void *sbrk(size_t incr) {

      extern uint8_t __heap_base__;

      extern uint8_t __heap_end__;

      static uint8_t *p = &__heap_base__;

      static uint8_t *newp;

      newp = p + incr;

      if (newp > &__heap_end__) {

        errno = ENOMEM;

        return (void *)-1;

      }

      return p = newp;

    }

    ....................

        char string[11];

        int data = 23;

        sprintf(string, ''%d'', data);

    Could you try this ?

    Anyway , we are opening a ticket for this.

      Best regards

                   Erwan

    In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question.
    henrik
    henrikAuthor
    Associate
    August 8, 2014
    Posted on August 08, 2014 at 17:25

    Hello Erwan,

    it compiles and sprintf seems to work like it should, afer a quick test.

    Thank you very much for the quick help!

    Best regards

    Henrik

    daewon279
    Visitor II
    August 22, 2014
    Posted on August 22, 2014 at 19:39

    Hello