C语言 如何在C中链接多个实现文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15622409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:49:43  来源:igfitidea点击:

How to link multiple implementation files in C

ccompilationheaderimplementation

提问by Kraken

I have a number of .cfiles, i.e. the implementation files say

我有很多.c文件,即实现文件说

  • main.c
  • A.c
  • B.c
  • 主文件
  • 交流电
  • 公元前

Where functions from any of the files can call any function from a different files. My question being, do I need a .hi.e. header file for each of A and B's implementation where each header file has the definition of ALLthe functions in A or B.

来自任何文件的函数可以调用来自不同文件的任何函数。我的问题是,我是否需要.h为 A 和 B 的每个实现都有一个ie 头文件,其中每个头文件都定义了A 或 B 中的所有函数。

Also, main.c will have both A.hand B.h #includedin it?

另外,main.c 中会有A.hB.h #included吗?

If someone can finally make it clear, also, how do I later compile and run the multiple files in the terminal.

如果有人最终可以说清楚,我以后如何在终端中编译和运行多个文件。

Thanks.

谢谢。

回答by Jonathan Leffler

Header contents

标题内容

The header A.hfor A.cshould only contain the information that is necessary for external code that uses the facilities defined in A.c. It should not declare static functions; it should not declare static variables; it should not declare internal types (types used only in A.c). It should ensure that a file can use just #include "A.h"and then make full use of the facilities published by A.c. It should be self-contained, idempotent (so you can include it twice without any compilation errors) and minimal. You can simply check that the header is self-contained by writing #include "A.h"as the first #includeline in A.c; you can check that it is idempotent by including it twice (but that's better done as a separate test). If it doesn't compile, it is not self-contained. Similarly for B.hand B.c.

A.hfor的标头A.c应仅包含使用 中定义的工具的外部代码所需的信息A.c。它不应该声明静态函数;它不应该声明静态变量;它不应声明内部类型(仅在 中使用的类型A.c)。它应该确保一个文件可以使用#include "A.h",然后充分利用A.c. 它应该是自包含的、幂等的(因此您可以将它包含两次而不会出现任何编译错误)并且是最小的。你可以简单地检查通过写报头是自包含#include "A.h"的第一个#include排队A.c; 您可以通过两次包含它来检查它是否是幂等的(但最好作为单独的测试来完成)。如果它不编译,它就不是自包含的。B.h和类似B.c

For more information on headers and standards, see 'Should I use #includein headers?', which references a NASA coding standard, and 'Linking against a static library', which includes a script chkhdrthat I use for testing self-containment and idempotency.

有关标头和标准的更多信息,请参阅“我应该#include在标头中使用吗?',它引用了 NASA 编码标准,以及 '链接到静态库',其中包括chkhdr我用于测试自包含和幂等性的脚本。

Linking

链接

Note that main.odepends on main.c, A.hand B.h, but main.citself does not depend on the headers.

请注意,main.o取决于main.c,A.hB.h,但main.c它本身不依赖于标头。

When it comes to compilation, you can use:

在编译时,您可以使用:

gcc -o program main.c A.c B.c

If you need other options, add them (most flags at the start; libraries at the end, after the source code). You can also compile each file to object code separately and then link the object files together:

如果您需要其他选项,请添加它们(大多数标志在开头;库在末尾,在源代码之后)。您还可以将每个文件分别编译为目标代码,然后将目标文件链接在一起:

gcc -c main.c
gcc -c A.c
gcc -c B.c
gcc -o program main.o A.o B.o

回答by Hyman

You must provide an header file just if what is declared in a .cfile is required in another .cfile.

如果一个.c文件中声明的内容在另一个.c文件中需要,您必须提供一个头文件。

Generally speaking you can have a header file for every source file in which you export all the functions declared or externsymbols.

一般来说,您可以为每个源文件都有一个头文件,您可以在其中导出所有声明的函数或extern符号。

In practice you won't alway need to export every function or every variable, just the one that are required by another source file, and you will need to include it just in the required file (and in the source paired with the specific header file).

在实践中,您并不总是需要导出每个函数或每个变量,只需导出另一个源文件所需的函数或变量,并且您只需将其包含在所需文件中(以及与特定头文件配对的源文件中) )。

When trying to understand how it works just think about the fact that every source file is compiled on its own, so if it's going to use something that is not declared directly in its source file, then it must be declared through an header file. In this way the compiler can know that everything exists and it is correctly typed.

当试图理解它是如何工作的时,只需考虑这样一个事实,即每个源文件都是独立编译的,因此如果要使用未在其源文件中直接声明的内容,则必须通过头文件声明。通过这种方式,编译器可以知道一切都存在并且它的类型是正确的。

回答by Youn Elan

It would depend on the compiler, but assuming you are using gcc, you could use something like this:

这将取决于编译器,但假设您使用的是 gcc,您可以使用以下内容:

gcc -Wall main.c A.c B.c -o myoutput

Look at http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html(first google answer) for more details. You could compile it into multiple object files/ libraries:

有关更多详细信息,查看http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html(第一个谷歌答案)。您可以将其编译为多个目标文件/库:

gcc -c main.c
gcc -c A.c
gcc -c B.c
gcc -o mybin main.o A.o B.o