gpt4 book ai didi

c++ - 为什么 C 允许我调用未声明的函数?

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:03 26 4
gpt4 key购买 nike

<分区>

我有两个文件:test1.c , 和 test2.c ,其中包含 main()功能。

测试1.c:

#include <stdio.h>  // printf() function declaration/prototype

// function definition
void say_hello() {
printf("\tHello, world!\n");
}

测试2.c:

#include <stdio.h>  // printf() function declaration/prototype
int main() {
printf("Inside main()\n");
say_hello();

return 0;
}

这是我的生成文件:

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我在屏幕上收到此警告:

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> .这些预处理器指令包括函数声明/原型(prototype),它们将它们的范围扩展到相应的 *.c 文件中。这就是我们能够使用它们的原因。没有函数声明/原型(prototype) == 在那个翻译单元中没有范围,或者直到现在我还这么认为。

然后我将上面的代码编译为C++代码: make CXX=g++

我在屏幕上看到这个错误:

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 编程语言的特权吗?是编译器的问题吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com