小编Joh*_*ozo的帖子

为什么我需要C头文件?

我承认,这是一个愚蠢的问题.代码会更好地解释它.得到这些文件:

你好ç:

#include <stdio.h>

void hello(char * s)
{
    printf("hello, %s\n", s);
}
Run Code Online (Sandbox Code Playgroud)

main.c中:

int main()
{
    hello("world!");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

生成文件:

test : main.o hello.o
    gcc -o test main.o hello.o

main.o : main.c
    gcc -c main.c

hello.o : hello.c
    gcc -c hello.c

.PHONY : clean
clean :
    -rm test
    -rm main.o
    -rm hello.o
Run Code Online (Sandbox Code Playgroud)

我可以"制作"和"./test"它,它的工作原理.我不应该在main.c中包含类似hello.h的东西,这样编译器才能知道函数原型吗?

我甚至没有在任何地方包含hello.c它只是工作!为什么main.c知道hello函数?

如果可行,我什么时候需要.h文件?我是C编程的新手,但我认为这个概念很容易掌握,现在我完全糊涂了.

c compiler-construction makefile header

4
推荐指数
2
解决办法
165
查看次数

标签 统计

c ×1

compiler-construction ×1

header ×1

makefile ×1