为什么C允许我调用未声明的函数? [英] Why does C allow me to call an undeclared function?
问题描述
我有两个文件: test1.c
和 test2.c
,其中包含 main()
函数.
I have two files: test1.c
, and test2.c
, which contains the main()
function.
test1.c:
#include <stdio.h> // printf() function declaration/prototype
// function definition
void say_hello() {
printf("\tHello, world!\n");
}
test2.c:
#include <stdio.h> // printf() function declaration/prototype
int main() {
printf("Inside main()\n");
say_hello();
return 0;
}
这是我的makefile:
And this is my makefile:
a.out: test1.o test2.o
$(CXX) -o a.out test1.o test2.o
test1.o: test1.c
$(CXX) -c test1.c
test2.o: test2.c
$(CXX) -c test2.c
现在应该清楚问题出在哪里:test2.c中的main()函数无需声明就可以调用say_hello()!我运行以下命令,以使用gcc编译器: make CXX = gcc
我在屏幕上收到此警告:
Now it should be clear where the problem lies: The main() function in test2.c calls say_hello() without declaring it!
I run the following command, to use the gcc compiler:
make CXX=gcc
I get this warning to the screen:
gcc -c test1.c
gcc -c test2.c
test2.c: In function ‘main’:
test2.c:16:3: warning: implicit declaration of function ‘say_hello’ [-Wimplicit-function-declaration]
say_hello();
^
gcc -o a.out test1.o test2.o
尽管* .o文件已编译并链接到可执行文件中.那真是怪了.想象一下,当我运行a.out文件时感到惊讶,并且看到 main()
成功调用了 say_hello()
,该函数未在main的翻译单元中声明,好像根本没有问题!我的理由是,由于 say_hello()
先前未在 test2.c
中声明,因此完全不应该由 main()
调用它..请注意,我已经在 #include< stdio.h>
中添加了注释.这些预处理器指令包括函数声明/原型,它们将其范围扩展到相应的* .c文件中.这就是为什么我们能够使用它们.在该翻译单元中没有函数声明/原型==没有作用域,所以直到现在我还是这么想.
Although the *.o files got compiled and linked into the executable. That's weird. Imagine my surprise when I run the a.out file, and I see that main()
successfully called say_hello()
, a function which is not declared inside of main's translation unit, as if there were no issue at all! I reason that since say_hello()
was not previously declared in test2.c
, it should not allowed to be called by main()
at all. Notice that I've added comments to the #include <stdio.h>
. These preprocessor directives include the function declarations/prototypes, which extend their scope into the corresponding *.c files. That is why we are able to use them. No function declaration/prototype == no scope in that translation unit, or so I thought until now.
然后我将上面的代码编译为C ++代码: make CXX = g ++
Then I compiled the above code as C++ code:
make CXX=g++
我在屏幕上看到此错误:
I get this error to the screen:
test2.c: In function ‘int main()’:
test2.c:16:13: error: ‘say_hello’ was not declared in this scope
say_hello();
^
makefile:18: recipe for target 'test2.o' failed
make: *** [test2.o] Error 1
g ++完成了应做的工作,并停止了编译过程.但是gcc没有做到这一点!这是怎么回事?它是C编程语言的风范吗?编译器有问题吗?
g++ does what it's supposed to do, and stops the compilation process. But gcc did not do this! What's going on? Is it a perk of the C programming language? Is it an issue with the compiler?
推荐答案
简单,因为C允许调用未声明的函数,而C ++不允许.无论哪种方式, gcc
都会警告您,您可能要认真对待警告.
Simple, because C allows undeclared functions to be called and C++ does not. Either way, gcc
warns you and you may want to take warnings seriously.
这篇关于为什么C允许我调用未声明的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!