Safely using C symbols and headers?
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
// overri开发者_StackOverflow中文版de 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
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.
精彩评论