我需要在编译C程序的头文件? [英] Do I need to compile the header files in a C program?

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

问题描述

有时我看到有人编这样一个C程序:

Sometimes I see someone compile a C program like this:

的gcc -o你好hello.c的hello.h

但我们都知道,我们只是需要把头文件到C程序,如:

But as we all know, we just need to put the header files into the C program like:

的#includesomefile

和编译C程序:的gcc -o你好的hello.c

所以,当我们需要编译的头文件?或者为什么?

So, when do we need to compile the header files? Or why?

推荐答案

首先,在一般的:

如果这些 .H 文件确实是典型的C风格的头文件(而不是作为完全不同的东西,只是碰巧与命名.H 扩展名),则没有,没有理由编译这些头文件独立。头文件旨在纳入实现文件,不供给到编译器作为独立的翻译单元。

If these .h files are indeed typical C-style header files (as opposed to being something completely different that just happens to be named with .h extension), then no, there's no reason to "compile" these header files independently. Header files are intended to be included into implementation files, not fed to the compiler as independent translation units.

由于典型的头文件通常仅包含可以在每个翻译单元被安全地反复声明,这是完全预期编译一个头文件都不会有任何危害后果。但在同一时间,它不会取得任何有用的东西。

Since a typical header file usually contains only declarations that can be safely repeated in each translation unit, it is perfectly expected that "compiling" a header file will have no harmful consequences. But at the same time it will not achieve anything useful.

基本上,编译 hello.h 作为一个独立的翻译单元等效于创建一个堕落 dummy.c 文件包括仅的的#includehello.h指令,并喂养 dummy.c 文件的编译器。这将编译,但它会成为没有意义的目的。

Basically, compiling hello.h as a standalone translation unit equivalent to creating a degenerate dummy.c file consisting only of #include "hello.h" directive, and feeding that dummy.c file to the compiler. It will compile, but it will serve no meaningful purpose.

其次,专门为GCC:

许多编译器将根据文件扩展名不同的对待文件。 GCC与 .H 的文件特殊待遇时,他们提供给作为编译器的命令行参数扩展。相反,把它当作一个普通翻译单元,GCC创建的 precompiled头的文件为 .H 文件。

Many compilers will treat files differently depending on the file name extension. GCC has special treatment for files with .h extension when they are supplied to the compiler as command-line arguments. Instead of treating it as a regular translation unit, GCC creates a precompiled header file for that .h file.

您可以读到这里:<一href=\"http://gcc.gnu.org/onlinedocs/gcc/$p$pcompiled-Headers.html\">http://gcc.gnu.org/onlinedocs/gcc/$p$pcompiled-Headers.html

所以,这是您可能会看到 .H 文件被直接加入海湾合作委员会的原因。

So, this is the reason you might see .h files being fed directly to GCC.

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

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