Ubuntu – Error linking standard header file in gcc

cgcc

I've a simple code – this works on other platforms but does not work in ubuntu.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=99;
char str[100];
itoa(99, str, 10);
return 0;
}

Trying to compile in using terminal in gcc with :

gcc test.c

But I get the error:

/tmp/ccJN77g6.o: In function `main':
test.c:(.text+0x35): undefined reference to `itoa'
collect2: ld returned 1 exit status

Why so? prototype for itoa is included in stdlib.h

Best Answer

itoa doesn't seem to be ANSI C++ and thus is very likely to be not supported by gcc.

According to this source, a the following workaround is suggested:

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

A standard-compliant alternative for some cases may be sprintf:

    sprintf(str,"%d",value) converts to decimal base.
    sprintf(str,"%x",value) converts to hexadecimal base.
    sprintf(str,"%o",value) converts to octal base.

A sprintf reference can be found here.

Related Question