安全使用 C 符号和头文件?
我正在寻找一种在没有标题或符号的情况下优雅降级的方法。考虑我的安全文件:
#import "FooHeader.h"
// override some method that needs a symbol from FooHeader
-(id)myImplementation:(FooSymbol)aSymbol
{
...
}
我想做的是在尝试导入 FooHeader.h 之前检查它是否存在。接下来,如果标头不存在,则符号 FooSymbol 也不可用,因此我也不想尝试编译该方法。像这样的事情:
#if HEADER_EXISTS(FooHeader)
#import "FooHeader.h"
#endif
// override some method that needs a symbol from FooHeader
#if SYMBOL_EXISTS(FooSymbol)
-(id)myImplementation:(FooSymbol)aSymbol
{
...
}
#endif
有人知道这样的机制是否可能吗?理想情况下,这可以在 C/C++/Objective-C 环境中工作。
谢谢
I am looking for a way to degrade gracefully in the absence of a header or symbol. Consider my safe file:
#import "FooHeader.h"
// override some method that needs a symbol from FooHeader
-(id)myImplementation:(FooSymbol)aSymbol
{
...
}
What I would like to do is check for the existence of FooHeader.h before attempting to import it. Next, if the header doesn't exist, it's also the case that the symbol FooSymbol is also not available so I wouldn't want to attempt to compile that method either. Something like this:
#if HEADER_EXISTS(FooHeader)
#import "FooHeader.h"
#endif
// override some method that needs a symbol from FooHeader
#if SYMBOL_EXISTS(FooSymbol)
-(id)myImplementation:(FooSymbol)aSymbol
{
...
}
#endif
Does anybody know if such a mechanism is possible? Ideally this would work in C/C++/Objective-C environments.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这通常是通过 autoconf 完成的。您创建一个名为configure.ac 的文件。在configure.ac中,添加AC_CHECK_HEADER([FooHeader.h], ....
当您在项目上运行./configure时,Makefile CFLAGS将获得-DHAVE_FooHeader定义。在C文件中,您使用#ifdef HAVE_FooHeader。
我没有方便的示例,但建议检查自动配置上的信息页面或在线查看自动工具教程。
This is typically done with autoconf. You create a file called configure.ac. In your configure.ac, you add AC_CHECK_HEADER([FooHeader.h], ....
When you run ./configure on your project, the Makefile CFLAGS will get a -DHAVE_FooHeader definition. Inside your C file you use #ifdef HAVE_FooHeader.
I don't have an example handy, but would suggest checking the info pages on autoconfig or looking at an autotools tutorial on-line.