C++ Visual Studio 中的 sprintf 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22954847/
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
sprintf error in visual studio
提问by wireless_lab
I am working on a UDP socket program in windows visual studio. I am getting an error for sprintf statement. How to correct it? My code is:
我正在 Windows Visual Studio 中开发一个 UDP 套接字程序。我收到 sprintf 语句的错误。如何纠正?我的代码是:
for (i = 0; i < 30;i++) //take-off
{
printf("send AT*REF:take off\n");
sprintf(command, "AT*REF=%d,290718208\r", seq++);
rc = sendto(sd, command, strlen(command) + 1, flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
if (rc<0) {
printf("%s: can not send data\n", argv[0]);
return(1);
}
}
The errors I am getting are :
我得到的错误是:
Error 1 error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
Error 1 error LNK2019: unresolved external symbol __imp__bind@12
Error 2 error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function _main
Error 3 error LNK2019: unresolved external symbol __imp__htons@4
Error 4 error LNK2019: unresolved external symbol __imp__sendto@24
Error 5 error LNK2019: unresolved external symbol __imp__socket@12
Error 6 error LNK2019: unresolved external symbol __imp__gethostbyname@4
Error 7 error LNK1120: 6 unresolved externals
回答by Bathsheba
printf
and its sister function sprintf
are considered unsafe due to the amount of undefined behaviourthey emit if used incorrectly.
printf
并且它的姊妹函数sprintf
被认为是不安全的,因为如果使用不当,它们会发出大量未定义的行为。
Visual Studio is disabling these functions by default.
Visual Studio 默认禁用这些功能。
But, since they are part of the C++ standard library, you canuse them. But Visual Studio will only allow you to do that if you include the line
但是,由于它们是 C++ 标准库的一部分,因此您可以使用它们。但是,如果您包含该行,Visual Studio 将只允许您这样做
#define _CRT_SECURE_NO_WARNINGS
before the relevant standard library headers are included.
在包含相关标准库头文件之前。
Alternatively, include _CRT_SECURE_NO_WARNINGS
in your project preprocessor settings (which is what I do).
或者,包括_CRT_SECURE_NO_WARNINGS
在您的项目预处理器设置中(这就是我所做的)。
(By the way, Visual Studio is emitting a very helpful error message in this instance. Do try to learn to interpret them.)
(顺便说一句,Visual Studio 在这种情况下发出了非常有用的错误消息。请尝试学习解释它们。)
回答by Michael Burr
For most of the errors in your question you need to add ws2_32.lib
to the library that the project links to (in the VS project's Linker | Input | Additional Dependencies
property).
对于您问题中的大多数错误,您需要添加ws2_32.lib
到项目链接到的库中(在 VS 项目的Linker | Input | Additional Dependencies
属性中)。
Other answers/comments have addressed the sprintf()
problem: either defined the macro _CRT_SECURE_NO_WARNINGS
or use a version of sprintf()
that is deemed safe by Microsoft, such as sprintf_s()
. I wish MSVC had a standard version of snprintf()
that can be used (maybe soon - they're adding quite a bit of C99 stuff to the toolchain).
其他答案/评论已解决该sprintf()
问题:定义宏_CRT_SECURE_NO_WARNINGS
或使用sprintf()
Microsoft 认为安全的版本,例如sprintf_s()
. 我希望 MSVC 有一个snprintf()
可以使用的标准版本(也许很快 - 他们正在向工具链添加相当多的 C99 内容)。