Professional #include contents

前端 未结 6 807
南笙
南笙 2021-01-28 18:07

I need to create an API that will allow my customer\'s developers to use a proprietary C module that will be released as a library (think .lib or .so -

6条回答
  •  悲哀的现实
    2021-01-28 19:06

    Other people mentioned documentation concerns, so I'll stay away from those :-P. Firstly, make sure you have sane include guards. personally I tend to like:

    FILENAME_20081110_H_, basically the filename in all caps, then the full date, this is help ensure that it is unique enough, even if there is another header in the tree which has the same name. (For example, you could imagine two config.h's included from 2 different lib directories having guards which use CONFIG_H_ or something like that and thus having a conflict. It doesn't matter what you choose, so long as it is likely to be unique.

    Also, if there is any chance this header will be used in a c++ project, please wrap your headers in blocks like this:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /* your stuff */
    
    #ifdef __cplusplus
    }
    #endif
    

    This will save some headaches with name mangling issues and make it so they don't have to wrap the header externally with these.

提交回复
热议问题