Do I need to compile the header files in a C program?

前端 未结 5 803
生来不讨喜
生来不讨喜 2020-12-23 12:32

Sometimes I see someone compile a C program like this:

gcc -o hello hello.c hello.h

As I know, we just need to put the header files into the C p

相关标签:
5条回答
  • 2020-12-23 12:54

    Firstly, in general:

    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.

    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.


    Secondly, specifically for GCC:

    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.

    You can read about it here: http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

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

    0 讨论(0)
  • 2020-12-23 12:57

    I think we do need preprocess(maybe NOT call the compile) the head file. Because from my understanding, during the compile stage, the head file should be included in c file. For example, in test.h we have

    typedef enum{
        a,
        b,
        c
    }test_t
    

    and in test.c we have

    void foo()
    {
        test_t test;
        ...
    }
    

    during the compile, i think the compiler will put the code in head file and c file together and code in head file will be pre-processed and substitute the code in c file. Meanwhile, we'd better to define the include path in makefile.

    0 讨论(0)
  • 2020-12-23 13:00

    Okay, let's understand the difference between active and passive code.

    The active code is the implementation of functions, procedures, methods, i.e. the pieces of code that should be compiled to executable machine code. We store it in .c files and sure we need to compile it.

    The passive code is not being execute itself, but it needed to explain the different modules how to communicate with each other. Usually, .h files contains only prototypes (function headers), structures.

    An exception are macros, that formally can contain an active pieces, but you should understand that they are using at the very early stage of building (preprocessing) with simple substitution. At the compile time macros already are substituted to your .c file.

    Another exception are C++ templates, that should be implemented in .h files. But here is the story similar to macros: they are substituted on the early stage (instantiation) and formally, each other instantiation is another type.

    In conclusion, I think, if the modules formed properly, we should never compile the header files.

    0 讨论(0)
  • 2020-12-23 13:04

    In some systems, attempts to speed up the assembly of fully resolved '.c' files call the pre-assembly of include files "compiling header files". However, it is an optimization technique that is not necessary for actual C development.

    Such a technique basically computed the include statements and kept a cache of the flattened includes. Normally the C toolchain will cut-and-paste in the included files recursively, and then pass the entire item off to the compiler. With a pre-compiled header cache, the tool chain will check to see if any of the inputs (defines, headers, etc) have changed. If not, then it will provide the already flattened text file snippets to the compiler.

    Such systems were intended to speed up development; however, many such systems were quite brittle. As computers sped up, and source code management techniques changed, fewer of the header pre-compilers are actually used in the common project.

    Until you actually need compilation optimization, I highly recommend you avoid pre-compiling headers.

    0 讨论(0)
  • 2020-12-23 13:05

    You don't need to compile header files. It doesn't actually do anything, so there's no point in trying to run it. However, it is a great way to check for typos and mistakes and bugs, so it'll be easier later.

    0 讨论(0)
提交回复
热议问题