mathieu wrote:
> Hi there,
>
> I am using the following piece of code (*) to determine whether or
> not a particular function exist. It is working fine most of the time,
> but I recently got a case where it shows limitation: builtins
> functions. For instance compiling it with a recent gcc version leads
> to:
>
> $ gcc -DCHECK_FUNCTION_EXISTS=strncasecmp -o CheckFunctionExists.o -
> c CheckFunctionExists.c
> CheckFunctionExists.c:3: warning: conflicting types for built-in
> function ‘strncasecmp’
>
> Any suggestion on a better way to do that ?
Yes: Read the documentation that comes with your C
implementation. "That's not automated," you may object,
but neither is the process of compiling, linking, and
looking for error messages. (If any; see below.)
> #ifdef CHECK_FUNCTION_EXISTS
>
> char CHECK_FUNCTION_EXISTS();
If the function *does* exist, and if this declaration
doesn't match the function's actual type, the behavior is
undefined. You may or may not get an error message; the
message may even amount to "no such function" even if a
function by that name does in fact exist. If you get no
error message you can conclude that the function exists,
but the presence of a message is not foolproof evidence
that the function is absent.
> #ifdef __CLASSIC_C__
Bad choice of macro name: All identifiers beginning
with two underscores are reserved.
> int main(){
> int ac;
> char*av[];
Missing a { here.
> #else
> int main(int ac, char*av[]){
> #endif
> CHECK_FUNCTION_EXISTS();
> if(ac > 1000)
> {
> return *av[0];
> }
> return 0;
> }
>
> #else /* CHECK_FUNCTION_EXISTS */
>
> # error "CHECK_FUNCTION_EXISTS has to specify the function"
The #error directive wasn't standardized until the ANSI
document was adopted in 1989, so if you still want to cater
to "classic C" you should probably avoid using it.
> #endif /* CHECK_FUNCTION_EXISTS */
Again: RTFM. It's a much better way, and will tell you
useful things like what the rewindWidget() function does,
what arguments it takes, and what special compiler flags
you need to use to get at it.
--
Eric Sosman
lid