简体   繁体   English

C99中的getchar_unlocked()隐式声明

[英]getchar_unlocked() implicit declaration in C99

Using getchar_unlocked and compiling with --std=c99 flag gives warningas follows- 使用getchar_unlocked并使用--std=c99标志进行编译会产生如下警告:

warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration] 警告:函数'getchar_unlocked'的隐式声明[-Wimplicit-function-declaration]

Does not give any warning if compiled without flag.Is there any way to work around with it ? 如果不带标志进行编译,则不会发出任何警告。是否有任何解决方法?

Starting from C99 you must have a visible function prototype before calling a function. 从C99开始,在调用函数之前必须具有可见的函数原型。 While the earlier C standard would just stupidly assume that any function unknown to the compiler has the format int func (params) , which in turn would cause severe bugs most of the time. 尽管较早的C标准只是愚蠢地假设编译器未知的任何函数都具有int func (params)格式,这反过来在大多数情况下会导致严重的错误。

Properly declare a prototype for getchar_unlocked and the bug will go away. 正确声明getchar_unlocked的原型,该错误将消失。

Note that there is no such function present in any standard library. 请注意,任何标准库中都不存在此类功能。 It seems you might have to include some non-standard library for the compiler to find the function. 看来您可能必须包括一些非标准的库,编译器才能找到该函数。

_unlocked versions of get... functions are POSIX extensions. _unlocked版本的get...函数是POSIX扩展。 They are not part of the standard functions of C99. 它们不是C99标准功能的一部分。 The full list of get... functions is given in 7.19.1.5: getwc , getwchar , getc , getchar , and gets (deprecated). get...函数的完整列表在7.19.1.5中给出: getwcgetwchargetcgetchargets (不推荐使用)。

When the function is not on this list, C99-compliant compiler must warn you that your program may not compile with other C99-compliant compilers. 当该函数不在此列表中时,符合C99的编译器必须警告您程序不能与其他符合C99的编译器一起编译。

Dialect selection options like -ansi and -std=c99 cause the compiler to define certain macros (in addition to altering the accepted dialect). 方言选择选项(例如-ansi-std=c99使编译器定义某些宏(除了更改可接受的方言外)。

Library header files react to those macros. 库头文件对这些宏做出反应。

Precisely how they react is quite system-dependent (the compiler doesn't provide a C library), but a common behavior you can broadly expect is that if you use one of these flags alone (without any other "feature selection macro"), it has the effect of hiding the declarations of functions, macros and other global symbols which are not in the specified ISO C dialect. 它们的反应方式完全取决于系统(编译器未提供C库),但是您可以普遍预期的普遍行为是,如果您单独使用这些标志之一(不使用任何其他“功能选择宏”),它具有隐藏不在指定ISO C语言中的函数,宏和其他全局符号的声明的作用。

ISO C knows nothing about getchar_unlocked . ISO C对getchar_unlocked The presence of such a declaration in <stdio.h> (normally an ISO C header) is a POSIX extension, which is basically nonconforming, since getchar_unlocked is an identifier that strictly conforming C programs can use, even if they include <stdio.h> . <stdio.h> (通常是ISO C标头)中存在这样的声明是POSIX扩展,这基本上是不符合要求的,因为getchar_unlocked是即使符合条件的C程序也可以使用的标识符,即使它们包含<stdio.h> When you use -ansi or -std=c99 , the <stdio.h> header listens up and whips itself into ISO-C-conforming shape, hiding such extensions. 当您使用-ansi-std=c99<stdio.h>标头会侦听并将其自身鞭打成符合ISO-C的形状,从而隐藏了此类扩展名。

On well-behaved POSIX systems, you can request that you want an ISO C dialect and that you want certain rudimentary 1990-ish POSIX features to be visible in header files, for instance like this: 在性能良好的POSIX系统上,您可以要求您使用ISO C语言, 希望某些基本的1990年代式POSIX功能在头文件中可见,例如:

gcc -std=c99 -D_POSIX_SOURCE ...
               ^^^^^ "feature selection macro"

There is a whole science to these feature selection macros, too broad for this question and answer; 这些特征选择宏具有一门完整的科学知识,对于这个问题和答案来说太宽泛了。 some forms of them have values, like -D_XOPEN_SOURCE=500 . 它们的某些形式具有值,例如-D_XOPEN_SOURCE=500 _POSIX_SOURCE doesn't need an argument; _POSIX_SOURCE不需要参数; it is just defined or not, but _POSIX_C_SOURCE is numeric. 它是否已定义,但_POSIX_C_SOURCE是数字。

I just checked glibc and Cygwin: on both, _POSIX_SOURCE is enough to reveal the getchar_unlocked declaration. 我刚刚检查了glibc和Cygwin:在这两者上, _POSIX_SOURCE足以显示getchar_unlocked声明。 It is quite old, dating back to POSIX.1 1996. 它很老,可以追溯到POSIX.1 1996。

Beware: on some systems, multiple feature selection macros don't play along reasonably; 当心:在某些系统上,多个功能选择宏不能合理发挥作用; they give you a set intersection rather than union, so that -D_POSIX_SOURCE and -D_BSD_SOURCE together end up meaning "Declare to me only those handful of functions that are specific to classic BSD that have been standardized in POSIX too", which means that next to nothing is declared. 它们为您提供了一个设置的交集而不是并集,因此-D_POSIX_SOURCE-D_BSD_SOURCE一起最终意味着“仅向我声明那些也已在POSIX中标准化的经典BSD的少数功能”。什么都没有宣布。

getchar_unlocked is not a C standard function. getchar_unlocked不是C标准函数。

Compiling it forcing c99 standard does not support it natively. 强制c99标准对其进行编译本身不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM