Any fundamental difference between source and header files in C?

前端 未结 4 1090
故里飘歌
故里飘歌 2020-12-02 07:40

I don\'t quite understand how things should be separated in C\'s source and header files. I often see many projects with two sets of files with the same name (sans the exten

4条回答
  •  温柔的废话
    2020-12-02 08:36

    What should be in headers and what should be in the source files?

    Typically headers contain one or more of the following:

    • Function declaration (except statics)
    • Variable declaration (typically global)
    • User defined type declaration (read struct, union etc.)
    • Macro definition

    Source files on the other hand have:

    • Function/variable definition
    • Static function declaration and definition (you don't want to expose these to your clients)
    • Variable definition
    • Some prefer to define inline functions (C99) in a header

    How do I implement this separation?

    The One Definition Rule is your friend.

    Remember, if you are writing a library, this is what your client gets to see. So, be helpful and provide all the information you can for them to use your library. The source files are typically compiled and supplied in binary form.

    And by the way, C does not have the concept of classes.

提交回复
热议问题