TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Large C codebase – how to organize code?

4 点作者 DictumMortuum将近 9 年前
I&#x27;m in the process of migrating the common libraries I used during my university years to github. They were the building blocks for larger implementations, like bplus trees, static hashing, etc.<p>I haven&#x27;t really coded in C since then, so when I looked at the source code after all those years I found a large number of bad practices and I&#x27;m slowly working towards fixing.<p>My question is - is there any guide towards organizing large C codebases that provides info like:<p>- How to name header guards? - How to name variables used for the same purpose for consistency? - How to name globals? - How to name the API functions? etc

2 条评论

copx将近 9 年前
Divide the code into modules which are as independent of each other as reasonably possible and thus can be reasoned about in isolation.<p>From the above follows that the modules do not hold global state and communicate with each other through well-defined interfaces - and not through global state.<p>Unfortunately C does not have an actual module system but only the &quot;unit of translation&quot; concept. In general you want two files per module e.g. my_module.c and my_module.h and prefix everything in there with my_module to avoid namespace conflicts e.g. my_module_new_foo(), my_module_update_foo(foo, MY_MODULE_FLAG | MY_MODULE_OTHER_FLAG)<p>EDIT: Note that the standard scheme to avoid holding global state in a module is that the module provides a function which allocates an &quot;instance&quot; of the module and all module functions work on such &quot;instances&quot; e.g.<p><pre><code> RNG_INSTANCE* r = rng_new(); double v = rng_uniform(r); ... rng_free(r);</code></pre>
DictumMortuum将近 9 年前
By the way, a friend suggested this guide, which is really interesting -&gt; <a href="https:&#x2F;&#x2F;matt.sh&#x2F;howto-c" rel="nofollow">https:&#x2F;&#x2F;matt.sh&#x2F;howto-c</a>