如何用C工作的头文件和源文件? [英] How do header and source files in C work?

查看:227
本文介绍了如何用C工作的头文件和源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了各种可能的重复,但是没有一个答案有下沉

I've perused the possible duplicates, however none of the answers there are sinking in.

TL;博士:你是如何源文件和头文件中的 C 有关?别项目在构建时理清声明/定义依赖含蓄?

tl;dr: How are source and header files related in C? Do projects sort out declaration/definition dependencies implicitly at build time?

我想了解编译器的理解的之间的关系 .C .H 文件。

I'm trying to understand how the compiler understands the relationship between .c and .h files.

由于这些文件:

header.h

int returnSeven(void);

由source.c

int returnSeven(void){
    return 7;
}

的main.c

#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(void){
    printf("%d", returnSeven());
    return 0;
}

请问这个烂摊子编译?目前,我正在做我的工作中的的NetBeans 7.0 GCC 从Cygwin的这远自动化构建的任务。当一个项目被编译将涉及理清项目文件的这种隐含纳入根据 header.h 由source.c $ C>?

Will this mess compile? I'm currently doing my work in NetBeans 7.0 with gcc from Cygwin which automates much of the build task. When a project is compiled will the project files involved sort out this implicit inclusion of source.c based on the declarations in header.h?

推荐答案

转换C源$ C ​​$ C文件的可执行程序一般分两步进行:编译连接

Converting C source code files to an executable program is normally done in two steps: compiling and linking.

首先,编译器将源$ C ​​$ C到目标文件( * 0 )。然后,链接器与静态链接库需要这些对象文件一起,并创建一个可执行程序。

First, the compiler converts the source code to object files (*.o). Then, the linker takes these object files, together with statically-linked libraries and creates an executable program.

在第一步中,编译器需要的编译单元,这通常是preprocessed源文件(这样,所有的标题内容的源文件,它的#include S)并将其转换为一个目标文件。

In the first step, the compiler takes a compilation unit, which is normally a preprocessed source file (so, a source file with the contents of all the headers that it #includes) and converts that to an object file.

在每个编译单元中,所使用的所有函数必须为宣布,让编译器知道该函数存在以及它参数是。在您的例子中,函数 returnSeven 的声明是在头文件 header.h 。当您编译的main.c ,您包括声明的头,使编译器知道 returnSeven 存在时,它编译的main.c

In each compilation unit, all the functions that are used must be declared, to let the compiler know that the function exists and what its arguments are. In your example, the declaration of the function returnSeven is in the header file header.h. When you compile main.c, you include the header with the declaration so that the compiler knows that returnSeven exists when it compiles main.c.

在连接器,它的工作,它需要找到每个功能的定义。每个功能都必须在目标文件中的一个准确定义一次 - 如果有包含相同的函数定义多个目标文件,链接器将因错误停止

When the linker does its job, it needs to find the definition of each function. Each function has to be defined exactly once in one of the object files - if there are multiple object files that contain the definition of the same function, the linker will stop with an error.

您函数 returnSeven 由source.c 定义(和函数定义在的main.c )。

Your function returnSeven is defined in source.c (and the main function is defined in main.c).

所以,总结一下,你有两个编译单元:由source.c 的main.c (用头文件,它包含)。编译这两个目标文件: source.o main.o中。第一个将包含 returnSeven 的定义,第二个的的定义。然后链接器将胶水这两个在一起的可执行程序。

So, to summarize, you have two compilation units: source.c and main.c (with the header files that it includes). You compile these to two object files: source.o and main.o. The first one will contain the definition of returnSeven, the second one the definition of main. Then the linker will glue those two together in an executable program.

关于联动:

外部链接内部链接即可。缺省情况下,函数具有外部连接,这意味着,编译器可以进行到连接器可见这些功能。如果你犯了一个函数静态,它具有内部连接 ​​- 它只有在它被定义(链接器将不知道它的存在)编译单元内可见。这可以为做一些内部的源文件,并且你想从程序的其余部分隐藏的功能是有用的。

There is external linkage and internal linkage. By default, functions have external linkage, which means that the compiler makes these functions visible to the linker. If you make a function static, it has internal linkage - it is only visible inside the compilation unit in which it is defined (the linker won't know that it exists). This can be useful for functions that do something internally in a source file and that you want to hide from the rest of the program.

这篇关于如何用C工作的头文件和源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆