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

查看:17
本文介绍了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 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天全站免登陆