Include a header in another header file

前端 未结 5 1752
别跟我提以往
别跟我提以往 2020-12-31 08:43

I\'ve defined a struct item in a .h file. Now I\'m defining another struct tPCB in another .h which is part of the same project, and I need the

相关标签:
5条回答
  • 2020-12-31 08:55

    You need to use #include. In C, everything is explicit; don't expect it to work by magic.

    0 讨论(0)
  • 2020-12-31 08:56

    If your .c #includes the two .h files in the proper order, it will work. That's probably what happened in the case you remember. The safest course is to #include every file that defines your dependencies, and rely on the include guards in each .h to keep things from being multiply defined.

    0 讨论(0)
  • 2020-12-31 09:00

    In your "another .h", #include <a .h file>.

    Elaboration:

    In the file that defines struct tPCB, you need to #include the file that defines struct item.

    0 讨论(0)
  • 2020-12-31 09:00

    Never ever put variable definitions (that is, allocating them) in a header file. That is bad for many different reasons, the two major ones being poor program design and floods of linker errors.

    If you need to expose a variable globally (there are not many cases where you actually need to do that), then declare it as extern in the h-file and allocate it in the corresponding C file.

    0 讨论(0)
  • 2020-12-31 09:11

    Sorry, there is no way in C that you can access a definition of a structure, in another header file without including that file (through an #include). Instructions for the #include follow.

    So, lets say that the header file that contains the definition of the item structure is called "item.h", and the header file that contains the definition of the tPCB structure in "tPCB.h". At the top of tPCB.h, you should put the following statement:

    #include "item.h"
    

    This should give the tPCB.h file access to all the definitions in item.h.

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