C语言 C 错误:对“_itoa”的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5428632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C Error: undefined reference to '_itoa'
提问by aytee17
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'
警告:'itoa' 的隐式声明
undefined reference to '_itoa'
对“_itoa”的未定义引用
I've already included stdlib.h, and am compiling with:
我已经包含了 stdlib.h,并且正在编译:
gcc -Wall -pedantic -ansi
Any help would be appreciated, thank you.
任何帮助将不胜感激,谢谢。
回答by Brian Roach
itoais not part of the standard. I suspect either -ansiis preventing you from using it, or it's not available at all.
itoa不是标准的一部分。我怀疑要么-ansi阻止您使用它,要么根本不可用。
I would suggest using sprintf()
我建议使用 sprintf()
If you go with the c99 standard, you can use snprintf()which is of course safer.
如果您使用 c99 标准,则可以使用snprintf()这当然更安全。
char buffer[12];
int i = 20;
snprintf(buffer, 12,"%d",i);
回答by Jens Gustedt
This here tells you that during the compilation phase itoais unknown:
这在这里告诉你在编译阶段itoa是未知的:
warning: implicit declaration of 'itoa'
警告:'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.
因此,如果您的系统上存在此函数,则您将缺少声明它的头文件。然后编译器假设它是一个函数,它接受不确定数量的参数并返回一个int.
This message from the loader phase
此消息来自加载程序阶段
undefined reference to '_itoa'
对“_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 itoaby a standard function.
因此,您或许应该按照 Brian 的建议替换itoa为标准函数。

