Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Error: undefined reference to '_itoa'

Tags:

c

itoa

I'm trying to convert an integer to a character to write to a file, using this line:

fputc(itoa(size, tempBuffer, 10), saveFile);

and I receive this warning and message:

warning: implicit declaration of 'itoa'

undefined reference to '_itoa'

I've already included stdlib.h, and am compiling with:

gcc -Wall -pedantic -ansi

Any help would be appreciated, thank you.

like image 495
aytee17 Avatar asked Mar 25 '11 04:03

aytee17


People also ask

What library is itoa in C?

C Programming/stdlib. h/itoa The itoa (integer to ASCII) function is a widespread non-standard extension to the standard C programming language. It cannot be portably used, as it is not defined in any of the C language standards; however, compilers often provide it through the header <stdlib.

What does itoa return?

When the radix is DECIMAL, itoa() produces the same result as the following statement: (void) sprintf(buffer, "%d", n); with buffer the returned character string. When the radix is OCTAL, itoa() formats integer n into an unsigned octal constant.

Does GCC support itoa?

itoa() is not ANSI-C defined and it not part of C++, and C32 is GCC based, and it is not really supported in GCC.


2 Answers

itoa is not part of the standard. I suspect either -ansi is preventing you from using it, or it's not available at all.

I would suggest using sprintf()

If you go with the c99 standard, you can use snprintf() which is of course safer.

char buffer[12];
int i = 20;
snprintf(buffer, 12,"%d",i);
like image 141
Brian Roach Avatar answered Sep 30 '22 02:09

Brian Roach


This here tells you that during the compilation phase itoa is unknown:

warning: implicit declaration of 'itoa'

so if this function is present on your system you are missing a header file that declares it. The compiler then supposes that it is a function that takes an unspecific number of arguments and returns an int.

This message from the loader phase

undefined reference to '_itoa'

explains that also the loader doesn't find such a function in any of the libraries he knows of.

So you should perhaps follow Brian's advice to replace itoa by a standard function.

like image 28
Jens Gustedt Avatar answered Sep 30 '22 01:09

Jens Gustedt