C头文件和编译/链接 [英] C header files and compilation/linking

查看:258
本文介绍了C头文件和编译/链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道头文件具有各种函数,结构等的前向声明,这些声明在调用" #include.c文件中使用,对吗?据我了解,权力分离"是这样发生的:

I know that header files have forward declarations of various functions, structs, etc. that are used in the .c file that 'calls' the #include, right? As far as I understand, the "separation of powers" occurs like this:

头文件:func.h

  • 包含函数的前向声明

  • contains forward declaration of function

int func(int i);

C源文件:func.c

  • 包含实际的函数定义

  • contains actual function definition

#include "func.h"

int func(int i) {
    return ++i ;
}

C源文件source.c(实际"程序):

C source file source.c (the "actual" program):

#include <stdio.h>
#include "func.h"

int main(void) {
    int res = func(3);
    printf("%i", res);
}

我的问题是:看到#include只是一个编译器指令,该指令将.h的内容复制到#include所在的文件中,.c文件如何知道如何实际执行功能?它得到的只是int func(int i);,那么它如何实际执行该功能?它如何获得对func的实际定义的访问权限?标头中是否包含某种指针",它表示那是我的定义,在那边!"?

My question is: seeing that the #include is simply a compiler directive that copies the contents of the .h in the file that #include is in, how does the .c file know how to actually execute the function? All it's getting is the int func(int i);, so how can it actually perform the function? How does it gain access to the actual definition of func? Does the header include some sort of 'pointer' that says "that's my definition, over there!"?

它如何工作?

推荐答案

Uchia Itachi给出了答案.这是链接器.

Uchia Itachi gave the answer. It's the linker.

使用GNU C编译器gcc,您将编译诸如

Using GNU C compiler gcc you would compile a one-file program like

gcc hello.c -o hello # generating the executable hello

但是如示例中所述编译两个(或多个)文件程序,则必须执行以下操作:

But compiling the two (or more) file program as described in your example, you would have to do the following:

gcc -c func.c # generates the object file func.o
gcc -c main.c # generates the object file main.o
gcc func.o main.o -o main # generates the executable main

每个目标文件都有外部符号(您可以将其视为公共成员).函数默认为外部,而(全局)变量默认为内部.您可以通过定义

Each object file has external symbols (you may think of it as public members). Functions are by default external while (global) variables are by default internal. You could change this behavior by defining

static int func(int i) { # static linkage
    return ++i ;
}

/* global variable accessible from other modules (object files) */
extern int global_variable = 10; 

当遇到对未在主模块中定义的函数的调用时,链接程序将为定义被调用函数的模块的输入提供的所有目标文件(和库)进行搜索.默认情况下,您可能有一些链接到程序的库,这就是使用printf的方式,它已经被编译到库中.

When encountering a call to a function, not defined in the main module, the linker searches all the object files (and libraries) provided as input for the module where the called function is defined. By default you probably have some libraries linked to your program, that's how you can use printf, it's already compiled into a library.

如果您真的有兴趣,请尝试一些汇编编程.这些名称与汇编代码中的标签等效.

If you are really interested, try some assembly programming. These names are the equivalent of labels in assembly code.

这篇关于C头文件和编译/链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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