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.
任何帮助将不胜感激,谢谢。
2 个解决方案
#1
22
itoa
is not part of the standard. I suspect either -ansi
is 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);
#2
2
This here tells you that during the compilation phase itoa
is 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 itoa
by a standard function.
因此,您应该遵循Brian的建议,用标准函数替换itoa。
#1
22
itoa
is not part of the standard. I suspect either -ansi
is 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);
#2
2
This here tells you that during the compilation phase itoa
is 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 itoa
by a standard function.
因此,您应该遵循Brian的建议,用标准函数替换itoa。