简体   繁体   English

为什么我可以在gcc -std = c11中使用gets()?

[英]Why can I use gets() in gcc -std=c11?

The gets() function has been removed from the C language. gets()函数已从C语言中删除。 No such function exists in the standard. 标准中不存在此类功能。

Yet I compile the following code: 然而,我编译以下代码:

#include <stdio.h>

int main (void)
{
  (void) gets (NULL);
}

using 运用

gcc -std=c11 -pedantic-errors -Wall -Wextra

and it compiles without giving any errors or warnings. 它编译时没有给出任何错误或警告。 Similarly, 同样的,

#include <stdio.h>

int gets;

int main (void)
{}

will not compile (error: 'gets' redeclared as different kind of symbol). 不会编译(错误:'获得'重新声明为不同类型的符号)。

In the standard 4. Conformance §6 we can read: 在标准4.一致性§6中,我们可以阅读:

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program 符合实现的实现可能具有扩展(包括附加库函数),前提是它们不会改变任何严格符合程序的行为

Given the above I don't think gcc is standard-compliant, even in pedantic mode. 鉴于上述情况,我认为即使在迂腐模式下,gcc也不符合标准。 Is there a reason for this? 是否有一个原因? Is this intentional or is it a bug? 这是故意还是错误?

GCC version 4.9.1. GCC版本4.9.1。

Edit: 编辑:

gcc --version
gcc (x86_64-win32-seh-rev1, Built by MinGW-W64 project) 4.9.1

gcc is just the compiler, not the entire implementation. gcc只是编译器,而不是整个实现。

On my system (Linux Mint 17.3, gcc 4.8.4, GNU libc 2.19), I get: 在我的系统(Linux Mint 17.3,gcc 4.8.4,GNU libc 2.19)上,我得到:

$ gcc -std=c11 -pedantic-errors -Wall -Wextra -c c.c
c.c: In function ‘main’:
c.c:5:3: error: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
   (void) gets (NULL);
   ^

To correctly diagnose the error, the implementation needs to be conforming. 要正确诊断错误, 实现需要符合要求。 That means both the compiler (which never provided gets in the first place) and the library. 这意味着这两种编译器(它从来没有提供gets摆在首位)和图书馆。

You're using a library that still provides the gets function. 您正在使用仍提供gets函数的库。 Because of that the implementation as a whole (which consists of the compiler gcc, the library, and a few other pieces) does not conform to C11. 因此,整个实现 (由编译器gcc,库和其他一些部分组成)不符合C11。

Bottom line: This is not a gcc issue, and there's not much that gcc can do about it. 结论:这不是一个gcc问题,gcc可以做的事情并不多。 (Well, it could issue a special-case diagnostic for gets , but then it would have to determine that it's not a valid call to a user-defined function with the same name.) (好吧,它可以gets发出一个特殊情况诊断,但是它必须确定它不是对具有相同名称的用户定义函数的有效调用。)

The key line of your code is: 您的代码的关键行是:

#include <stdio.h>

Did you update your system's C library and headers? 您是否更新了系统的C库和标题? They're also part of the C implementation, along with the compiler. 它们与编译器一起也是C实现的一部分。

update this may not be an answer to the question, I try to make it informational. 更新这可能不是问题的答案,我试图使其信息化。

I happened to find that gcc mentioned gets is not following C11 standard for some library issue glibc 2.16 . 我偶然发现提到获取的gcc没有以下一些问题库标准C11 glibc 2.16

See gcc supporting status of C11: https://gcc.gnu.org/wiki/C11Status 请参阅gcc支持C11的状态: https//gcc.gnu.org/wiki/C11Status 从上面的链接歪曲

But I cannot find the definition of "library issue" and current status for other versions of glibc. 但我找不到“库问题”的定义和其他版本的glibc的当前状态。

So I tried on my machine ubuntu16.04 with gcc version 5.3.1 20160413 , glibc version Ubuntu GLIBC 2.23 We can get enough warning on compile time, but it's still OK to execute the output object file for "Backwards compatibility". 所以我试用了我的机器ubuntu16.04和gcc版本5.3.1 20160413 ,glibc版本Ubuntu GLIBC 2.23我们可以在编译时得到足够的警告,但是仍然可以执行输出对象文件的“向后兼容性”。

warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
warning: the `gets' function is dangerous and should not be used.

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

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