1

I'm trying out some of the solutions to my question.

Say I'm running on a system that implements itoa. I have this code:

char *my_itoa (int a, char *b, int c)
{
    return itoa(a, b, c);
}

char* __attribute__ ((weak)) itoa(int a, char* b, int c)
{
     strcpy(b, "No itoa");
}

If I call my_itoa(10, str, 10) with the above code, puts(str) gives me "No itoa".

However, if I simply write:

char *my_itoa (int a, char *b, int c)
{
    return itoa(a, b, c);
}

and call my_itoa(10, str, 10), puts(str) gives me "10".

My understanding was that __attribute__ ((weak)) would implement a function if it was not already implemented. Is that its true behavior? What's going on here?

Community
  • 1
  • 1
MD XF
  • 7,860
  • 7
  • 40
  • 71
  • Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker. Are all those things true (ELF, GNU assembler and linker)? – Doug Currie Mar 15 '17 at 16:50
  • @DougCurrie I'm using MinGW, so GNU but not ELF. – MD XF Mar 15 '17 at 16:51

0 Answers0