windows 我如何使用 errorno 和 _get_errno?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2270047/
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
How do I use errorno and _get_errno?
提问by T.T.T.
Calling system()to run an external .exe and checking error code upon errors:
调用system()运行外部 .exe 并在出现错误时检查错误代码:
#include <errno.h>
#include <stdlib.h>
function()
{
errno_t err;
if( system(tailCmd) == -1) //if there is an error get errno
{
//Error calling tail.exe
_get_errno( &err );
}
}
First two compile errors:
前两个编译错误:
error C2065: 'err' : undeclared identifier
error C2065: 'errno_t' : undeclared identifier
Not sure why as I am including the required and optional header files?
Any help is appreciated. Thank You.
不知道为什么,因为我包含了必需和可选的头文件?
任何帮助表示赞赏。谢谢你。
回答by shinkou
回答by Jonathan Leffler
In the world of Standard C, the type 'errno_t
' is defined by TR24731-1 (see Do you use the TR 24731 'safe' functions?for more information) and you have to 'activate it' by defining '__STDC_WANT_LIB_EXT1__
'.
在标准 C 的世界中,类型“ errno_t
”由 TR24731-1 定义(有关更多信息,请参阅您使用 TR 24731 的“安全”功能吗?)并且您必须通过定义“激活它__STDC_WANT_LIB_EXT1__
”。
However, you appear to be working on Windows (judging from 'tail.exe', and also the non-standard '_get_errno()
'). The rules there may depend on the C compiler you are using.
You should be able to chase down the information from this MSDN article on 'Security Enhancements in the CRT'. My impression was that it should be defined unless you actively suppress the feature, so check out whether you are actively suppressing it in your compilations.
但是,您似乎在 Windows 上工作(从“tail.exe”以及非标准的“ _get_errno()
”判断)。那里的规则可能取决于您使用的 C 编译器。您应该能够从这篇关于“CRT 中的安全增强”的MSDN 文章中找到信息。我的印象是除非您主动抑制该功能,否则应该定义它,因此请检查您是否在编译中主动抑制它。
Be aware that the MSVC definition of functions such as vsnprintf_s()
do not match the TR24731-1 definitions:
请注意 MSVC 等函数vsnprintf_s()
的定义与 TR24731-1 定义不匹配:
MSDN:
微软:
int vsnprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format,
va_list argptr
);
TR 24731-1:
TR 24731-1:
int vsnprintf_s(
char * restrict s,
rsize_t n,
const char * restrict format,
va_list arg
);
The difference is not just a question of type aliases or qualifiers (rsize_t
, restrict
) - there are two sizes in the MS version and one in the Standard version. So much for standardization!
区别不仅仅是类型别名或限定符 ( rsize_t
, restrict
) 的问题 - MS 版本中有两种尺寸,而标准版中有一种。标准化就这么多!
回答by abc
Just use 'errno' without any declaration. It is a macro that expands to an int value.
只需使用'errno'而无需任何声明。它是一个扩展为 int 值的宏。