What methods are there to modularize C code?

后端 未结 8 1613
我在风中等你
我在风中等你 2021-02-05 15:48

What methods, practices and conventions do you know of to modularize C code as a project grows in size?

8条回答
  •  臣服心动
    2021-02-05 16:16

    OO techniques can be applied to C code, they just require more discipline.

    • Use opaque handles to operate on objects. One good example of how this is done is the stdio library -- everything is organised around the opaque FILE* handle. Many successful libraries are organised around this principle (e.g. zlib, apr)
    • Because all members of structs are implicitly public in C, you need a convention + programmer discipline to enforce the useful technique of information hiding. Pick a simple, automatically checkable convention such as "private members end with '_'".
    • Interfaces can be implemented using arrays of pointers to functions. Certainly this requires more work than in languages like C++ that provide in-language support, but it can nevertheless be done in C.

提交回复
热议问题