简体   繁体   English

C makefile和标头混淆

[英]C makefile and header confusion

I have a couple of general questions about Make files, headers, and including files in C, and am hoping to get an easy to interpret answer because all the tutorials go from "ok I understand this" to "what the heck is this" just in a couple lines. 我对Make文件,标头以及包含C语言中的文件有几个一般性问题,希望得到一个易于理解的答案,因为所有教程都从“确定,我明白了”变成“这到底是什么”在几行中。 Lets say I have a program I want to create a makefile for. 可以说我有一个要为其创建文件的程序。

**fileA.c** contains the main and calls functions in **fileB.c** and **fileC.c**

**fileB.c** contains getopts and stdlib but no other file or function from other file

(so normal headers, correct? nothing linking to any other file since it doesn't call anything. Or should it have fileA.c header since it is called from there?) (因此,正常的标头是正确的吗?因为它不会调用任何文件,所以没有链接到任何其他文件。或者应该从那里调用它而具有fileA.c标头吗?)

**fileC.c** contains functions that call header1.h (given header file for library)

I am just confused what headers are included and what I put in the make file to support make/clean functions. 我只是感到困惑,包括哪些标头,以及我放入make文件中以支持make / clean功能的内容。 Since fileA.c calls functions from fileB.c and fileC.c do i create headers for them and #include them in fileA.c or do I write that into the makefile? 由于fileA.cfileB.cfileC.c调用函数我该为他们创造页眉和#包括他们fileA.c还是我写入生成文件? If I include a header for something in the file and then create a makefile for the file, does it go to the pre-processor twice? 如果我在文件中包含某些内容的标头,然后为该文件创建一个makefile,它是否两次进入预处理器?

I think I may just be confused in the actual purpose of the makefile vs the header file, and that is causing me to lose my mind. 我想我可能只是对makefile和header文件的实际目的感到困惑,这使我迷失了方向。

Thanks for taking the time to read and any help would be greatly appreciated. 感谢您抽出宝贵的时间阅读本书,任何帮助将不胜感激。

edit: 编辑:

for reference, 以供参考,

fileA.o: fileA.c fileA.h
${CC} ${CFLAGS} -c fileA.c

so, since fileA isn't called by anything this would be correct (I do pass address from fileA to both fileB and fileC)? 因此,由于fileA没有被任何东西调用,这是正确的(我确实将地址从fileA传递到了fileB和fileC)吗?

would fileB.o: contain fileB.c fileB.h and thats it? 将fileB.o:包含fileB.c fileB.h就是这样吗? or would it have the other headers? 还是会有其他标题?

The first step for you, is to forget about the makefiles. 对您来说,第一步是忘记makefile。 Just setup your project in C, and to compile it, run the gcc commands by hand, or if you get tired of that, write them one after another in a shell script and run that. 只需使用C设置您的项目,然后进行编译,手动运行gcc命令,或者如果您对此感到厌倦,则可以在shell脚本中逐个编写它们并运行它。

Make sure it all runs to your satisfaction, before you attempt to write a Makefile . 在尝试编写Makefile之前,请确保一切运行令人满意。

You will probably have calls like 您可能会收到类似的电话

gcc -c fileB.c

and

gcc fileA.o fileB.o fileC.o -o fileA

The Makefile cannot be written if you are having trouble doing it "by hand", you understand? 如果您无法手动执行Makefile则无法编写该Makefile

Now, there comes the time to write the Makefile . 现在,是时候编写Makefile To do that, you need "rules". 为此,您需要“规则”。 A rule is something like this: 规则是这样的:

file_to_write:  all the files that impact file_to_write
    command to create the file to write

Each rule should describe how to create one file. 每条规则应描述如何创建一个文件。 That file is on the left of : . 该文件是在左边: It is called a "target". 它称为“目标”。 On the right of : are listed the "prerequisites". :右边列出了“先决条件”。

Listen carefully now. 现在仔细听。 The prerequisites, are all the files, that, if changed, could result in the target behaving differently. 前提条件是所有文件,如果更改,可能会导致目标文件的行为有所不同。

Below that, is the "recipe" - that is the command that reads the prerequisites and produces the target. 在其下方的是“食谱”,即读取先决条件并生成目标的命令。

If the recipe reads some files, most likely those files must be in the prerequisite list. 如果配方读取某些文件,则很可能这些文件必须在先决条件列表中。

So for example, you will have a rule to produce fileA like this: 因此,例如,您将具有如下规则来生成fileA

fileA: fileA.o fileB.o fileC.o Makefile
    gcc fileA.o fileB.o fileC.o -o fileA

You see, if any file....o were different, fileA could be different too, that's why they are in the prerequisites. 您会看到,如果有任何file....o有所不同, fileA也可能会有所不同,这就是为什么它们位于先决条件中的原因。 Also, you realize, that if Makefile itself were different, then fileA could end up different, because, the recipe could have changed. 而且,您意识到,如果Makefile本身不同,则fileA可能最终会不同,因为配方可能已更改。

In theory, if you changed the gcc file, for a newer revision for example, that could change the functionality of fileA as well. 从理论上讲,如果您更改了gcc文件(例如,对于较新的修订版),则也可能会更改fileA的功能。 But typically we don't include tools files like gcc that change infrequently and are not under source control. 但通常我们不包括不经常更改且不受源代码控制的工具文件,例如gcc

Of course, you will have another rule for fileA.o . 当然,您将对fileA.o有另一条规则。 This rule will probably have fileA.c and some headers that are read when fileA.c is compiled, as prerequisites. 前提条件是,该规则可能包含fileA.c和一些在编译fileA.c时读取的标头。 And so forth. 依此类推。

fileA.c does not need a rule. fileA.c不需要规则。 Why? 为什么? Because it is not created by the build, it is always already there. 因为它不是由构建创建的,所以它始终已经存在。

If fileA.c is dependent on fileB.c and fileC.c your makefile should be: 如果fileA.c依赖于fileB.c和fileC.c,则您的makefile应该为:

fileA: fileB.o fileC.o fileA.o
    $(CC) fileB.o fileC.o fileA.o -o fileA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM