当函数未被前向声明时,GCC在编译阶段不会抛出错误

时间:2013-07-09 07:36:41

标签: c unix gcc

好的,

我的目录中有3个文件。

的main.c

#include <stdio.h>
int main(int a, int b, int c)
{
    aprint();
    bprint();
}

交流转换器

#include <stdio.h>

void aprint()
{
    printf("hey This is a.c");
}

b.c

#include <stdio.h>
void bprint()
{
   printf("This is b.c");
}

我还没有创建任何头文件。 我刚用“gcc main.c a.c b.c”编译 我没有得到任何错误。我想知道发生了什么? gcc是否只是假设链接阶段的一切都没问题,为什么gcc在编译期间没有抛出错误?

3 个答案:

答案 0 :(得分:7)

使用-Wall标志启用警告,您将看到警告:对函数bprint()的隐式调用和对函数aprint()的隐式调用。基本上编译器在链接器阶段识别此函数,这不会产生任何错误。

答案 1 :(得分:3)

C语言的C89 / 90版本不需要向前声明函数。 C语言的C99版本确实需要向前声明函数。您是在C89 / 90模式还是在C99模式下编译代码?

请注意,即使在C99模式下,编译器也可能会报告真正的错误(即违反约束)仅作为警告。如果您希望GCC在将约束违规报告为错误时变得更加严格,请使用-pedantic-errors开关运行它。

答案 2 :(得分:2)

我给你发了警告:

notroot@ubuntu:~/qweqwe$ gcc main.c a.c b.c -Wall
main.c:2:5: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
main.c:2:5: warning: third argument of ‘main’ should probably be ‘char **’ [-Wmain]
main.c: In function ‘main’:
main.c:4:1: warning: implicit declaration of function ‘aprint’ [-Wimplicit-function-declaration]
main.c:5:1: warning: implicit declaration of function ‘bprint’ [-Wimplicit-function-declaration]
main.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]