简体   繁体   English

C标头中包含include

[英]C includes of includes in headers

I'm going rounds with the compiler right now, and I want to make sure the problem isn't a fundamental misunderstanding of how header files. 我现在正与编译器进行全面交流,我想确保问题不是对头文件的方式有根本的误解。 If I include a header file, and that header file has includes in it (like <stdbool.h> or <stdio.h> , etc...), there shouldn't be any issue on the dependent C file, right? 如果我包含一个头文件,并且该头文件中包含头文件(例如<stdbool.h><stdio.h>等),则从属C文件应该没有任何问题,对吗? The preprocessor should just insert the assembled code accordingly when I call my makefile, by my understanding. 据我所知,当我调用我的makefile文件时,预处理器应该相应地插入汇编代码。 Am I mistaken? 我错了吗?

To reiterate: 重申:

  • Say I have a main.c with prototype.h . 说我有一个main.cprototype.h

  • prototype.h has in it all of my usual includes for libraries and what-not. prototype.h包含了我通常所有的库和其他内容。

  • I have a couple other C files ( secondary.c and tertiary.c , for instance), both of which need the usual libraries, as well, and may or may not need some of the prototypes and also have their own header files. 我还有几个其他的C文件(例如, secondary.ctertiary.c ),它们也都需要常用的库,并且可能需要也可能不需要某些原型,并且还具有自己的头文件。

All I'd need to do is include prototype.h in each of the C files, correct? 我需要做的就是在每个C文件中包含prototype.h ,对吗?

Also, in that instance, if I were making .o file using the -c flag of gcc in my makefile , would I need to update the dependency in the target of the makefile ? 另外,在那种情况下,如果我在makefile使用gcc-c标志来创建.o文件,是否需要更新makefile目标中的依赖项?

I thought I had a solid handle on this when I started, but now I'm thoroughly confused. 我刚开始时以为我对此有很强的把握,但现在我完全感到困惑。

That is questions with multiple answers. 那是有多个答案的问题。

In simple case you only include headers if they are used in .c file. 在简单情况下,仅在.c文件中使用标头时才包含标头。 Headers are dependencies for compilation so for Make you just put it in the same bucket where .c files using those. 标头是编译的依赖项,因此对于Make,您只需将其放在使用.c文件的相同存储区中即可。

For big projects to speedup compilation you may use what called 'precompiled header'. 为了加快大型项目的编译速度,您可以使用“预编译头文件”。 You must include the same header file into every source file, compiler will only process that header once. 您必须在每个源文件中包含相同的头文件,编译器将只处理该头一次。 That is very useful when headers are complicated (eg boost library) 当标头很复杂(例如boost库)时,这非常有用

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

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