views:

44

answers:

1

I maintain an open source program that builds with autoconf.

Right now I'm having a problem with some of my users. They are using a pre-distributed VM from an organization that has an incorrect prototype for strchr in it. Their prototype is:

char *strchr(char *,int c);

when of course we know it should be:

char *strchr(const char *s,int c);

(which is itself broken, as the output should really be const char *, but then you couldn't modify what it gives you if you passed in a char * and not a const char *, but I digress.

My question: is there a way to create an autoconf macro that determines which prototype is in use and use it accordingly? I'd rather not make my code say:

v = strchr((char *)s,c);

Thanks!

+3  A: 

You should be able to set up a configure test that attempts to call the const char* version (NOT using a literal since there's an implicit conversion to char*). Configure will tell you if it compiled or not so you can #define something based on that and use it to make a decision in your code (preferably in some sort of wrapper/utility class).

For example something like (untested):

AC_TRY_COMPILE([#include <cstring>],
 [const char* str = "Test"; strchr(str, 't');],
 conforming_strchr=yes,
 conforming_strchr=no)
if test "$conforming_strchr" = yes; then
  AC_DEFINE(HAVE_CONFORMING_STRCHR, 1,
   [define to 1 if strchr takes const char*])
fi
Mark B
Thanks. I'll give it a try.
vy32
UYou have an extra close parent in line 4. Other than that, it worked. Thanks!
vy32