warning: ‘gets’ is deprecated [-Wdeprecated-declarations] gets(a); ^~~~ In file included from /usr/include/stdio.h:862:0, from a.c:1: /usr/include/x86_64-linux-gnu/bits/stdio2.h:232:1: note: declared here gets (char *__str) ^~~~ a.c:6:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&t); ^~~~~~~~~~~~~~ a.c:11:5: warning: ignoring return value of ‘gets’, declared with attribute warn_unused_result [-Wunused-result] gets(a); ^~~~~~~ /tmp/cc8Uj9xp.o: In function `main': a.c:(.text.startup+0x47): warning: the `gets' function is dangerous and should not be used.
时间: 2023-10-21 09:05:26 浏览: 123
这是一个编译器警告,指出你使用过时的函数`gets`。警告建议不要使用`gets`函数,因为它存在安全漏洞,可能导致缓冲区溢出。相反,建议使用更安全的替代函数,比如`fgets`。另外,还有一个警告指出你忽略了`scanf`和`gets`函数的返回值,在某些情况下这可能是个问题,你可以考虑处理这些返回值。
相关问题
成功编译,但有警告信息. ___2.cpp: In function 'int main()': ___2.cpp:7:5: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] gets(str); ^ ___2.cpp:7:13: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] gets(str); ^ /tmp/cce6zYD0.o: In function `main': ___2.cpp:(.text.startup+0x15): warning: the `gets' function is dangerous and should not be used.
这个警告信息是因为使用了 `gets()` 函数,这个函数已经被标记为不安全的函数,因为它无法限制输入的长度,存在缓冲区溢出的风险。因此,在新的 C 标准中,这个函数已经被废弃了,建议使用更安全的函数 `fgets()` 来代替它。
如果你使用的是老的编译器,那么可能还支持 `gets()` 函数,但是在新的编译器中,为了安全起见,不建议使用 `gets()` 函数。
如果你想消除这个警告,可以将程序中的 `gets()` 函数替换为 `fgets()` 函数,如下所示:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[80];
fgets(str, 80, stdin); // 输入字符串
int len = strlen(str);
if (str[len-1] == '\n') {
str[len-1] = '\0'; // 去掉换行符
}
printf("输入的字符串为: %s\n", str);
return 0;
}
```
这样就可以避免使用不安全的 `gets()` 函数了。需要注意的是,在使用 `fgets()` 函数读入字符串后,输入缓冲区中可能会有一个换行符,因此需要在字符串末尾手动添加一个 '\0',并将换行符替换为 '\0'。
In file included from /usr/include/c++/9/backward/strstream:50, from /home/ubuntu/librealsense/src/gl/upload.cpp:24: /usr/include/c++/9/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp] 32 | #warning \ | ^~~~~~~
这个警告是由C++编译器生成的,它告诉你在你的代码中使用了已经过时或将来可能被移除的头文件。在这种情况下,警告是关于`strstream`头文件的使用。编译器建议你使用具有相同功能的非过时接口来替代它。
如果你想禁用这个警告,你可以在编译时使用选项`-Wno-deprecated`。这将告诉编译器忽略这个警告。
然而,为了保持代码的可维护性和可移植性,建议你使用推荐的替代头文件和接口,以防止在将来的版本中出现不兼容的问题。你可以查看`backward_warning.h`文件,其中列出了替代头文件和接口的信息。