【发布时间】:2021-05-03 14:38:47
【问题描述】:
这个问题不是如何删除警告
我正在写一个shell。我提到了这个source。我在我的代码中使用了与他一样的标题(以相同的顺序)。
编译他的代码时,我没有收到关于getline 的隐式声明 的任何警告。但是当我编译我的时,它确实被抛出了。
手册页建议使用#define _GNU_SOURCE,并添加它从我的代码中删除了警告。
那么为什么博客中的代码没有发出警告,因为他没有使用#define _GNU_SOURCE?
这是最少的代码(我复制了上面提到的所有标题)
// #define _GNU_SOURCE
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
ssize_t bytes_read;
size_t input_buffer_size = 1024;
char *user_input = (char *)malloc(input_buffer_size * sizeof(char));
while (1)
{
printf("> ");
bytes_read = getline(&user_input, &input_buffer_size, stdin);
printf("%s\n", user_input);
}
return 0;
}
这是我使用的编译过程...
gcc -std=c11 -o bin/shell src/shell.c
如果我将第一行注释掉,则会出现以下错误。
src/shell.c: In function ‘main’:
src/shell.c:18:18: warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]
18 | bytes_read = getline(&user_input, &input_buffer_size, stdin);
| ^~~~~~~
【问题讨论】:
-
可能使用了不同版本的编译器。
-
你的代码有什么不同?
-
在收到警告时您使用了哪些命令行参数?另外,
cc --version | head -n1打印什么? -
显示代码 (minimal reproducible example),因为它是在问题正文中。
-
有一个非标准函数
getline,不符合标准的编译器可能会喷入标准头文件。如果您尝试编写自己的名为getline的函数,则必须通过例如gcc -std=c11 -pedantic-errors来阻止库之一的存在。或者,如果要使用预制的getline,则需要使用非标准库gcc -std=gnu11进行编译。