以灵活且不显眼的方式扩展 C 语言的 API
我正在尝试向 API 添加一些附加功能。但是,我希望这些附加函数位于我的外部库中,而不是与原始库的代码混合。
当我需要从我的函数访问上述 API 的静态函数时,问题就出现了。我当然不能,所以我看到的唯一解决方案是将这些函数的代码复制到我的 API 中,或者在原始 API 中使它们成为非静态的。出于显而易见的原因,两者对我来说都不是太好的选择。
更准确地说:
original_api.c
void some_function() -> uses some_helper_function()
static some_helper_function()
my_api_extension.c
void some_extended_function() -> needs to use some_helper_function from original_api.c, but can't
您能否建议哪种方法是处理此问题的最灵活的方法?
我想指出的是,它仅与 C 相关,与 C++ 无关。
I'm trying to add some additional functionality to an API. However, I'd like these additional functions to be in an external library of mine and not mixed with the original library's code.
The problem comes when I need to access static functions of the mentioned API from my functions. Of course I can't, so the only solution I see is either to copy these functions' code into my API or to make them non-static in the original API. Both are not too good options for me for obvious reasons.
More precisely:
original_api.c
void some_function() -> uses some_helper_function()
static some_helper_function()
my_api_extension.c
void some_extended_function() -> needs to use some_helper_function from original_api.c, but can't
Could you suggest which would be the most flexible way to handle this?
I'd like to point out that it's related to C only, not C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#include
文件中声明它,该文件可供扩展使用,但不适用于整个世界。struct
。像这样的东西:
struct
with pointers to these functions. Declare it in a separate#include
file, available to the extension, but not to the entire world.struct
in your extension.Something like this: