Google

Back to the Main Page.

ac_compile_check_sizeof

Download the M4 Source.

Synopsis

AC_COMPILE_CHECK_SIZEOF(TYPE [, HEADERS [, EXTRA_SIZES...]])

Version

1.1.1.1 (2001/07/26)     Miscellaneous @ ac-archive-0.5.39

Author

Kaveh Ghazi <ghazi@caip.rutgers.edu>

Description

This macro checks for the size of TYPE using compile checks, not run checks. You can supply extra HEADERS to look into. the check will cycle through 1 2 4 8 16 and any EXTRA_SIZES the user supplies. If a match is found, it will #define SIZEOF_`TYPE' to that value. Otherwise it will emit a configure time error indicating the size of the type could not be determined.

The trick is that C will not allow duplicate case labels. While this is valid C code:

      switch (0) case 0: case 1:;

The following is not:

      switch (0) case 0: case 0:;

Thus, the AC_TRY_COMPILE will fail if the currently tried size does not match.

Here is an example skeleton configure.in script, demonstrating the macro's usage:

      AC_PROG_CC
      AC_CHECK_HEADERS(stddef.h unistd.h)
      AC_TYPE_SIZE_T
      AC_CHECK_TYPE(ssize_t, int)

      headers='#ifdef HAVE_STDDEF_H
      #include <stddef.h>
      #endif
      #ifdef HAVE_UNISTD_H
      #include <unistd.h>
      #endif
      '

      AC_COMPILE_CHECK_SIZEOF(char)
      AC_COMPILE_CHECK_SIZEOF(short)
      AC_COMPILE_CHECK_SIZEOF(int)
      AC_COMPILE_CHECK_SIZEOF(long)
      AC_COMPILE_CHECK_SIZEOF(unsigned char *)
      AC_COMPILE_CHECK_SIZEOF(void *)
      AC_COMPILE_CHECK_SIZEOF(size_t, $headers)
      AC_COMPILE_CHECK_SIZEOF(ssize_t, $headers)
      AC_COMPILE_CHECK_SIZEOF(ptrdiff_t, $headers)
      AC_COMPILE_CHECK_SIZEOF(off_t, $headers)

M4 Source Code
AC_DEFUN([AC_COMPILE_CHECK_SIZEOF],
[changequote(<<, >>)dnl
dnl The name to #define.
define(<<AC_TYPE_NAME>>, translit(sizeof_$1, [a-z *], [A-Z_P]))dnl
dnl The cache variable name.
define(<<AC_CV_NAME>>, translit(ac_cv_sizeof_$1, [ *], [_p]))dnl
changequote([, ])dnl
AC_MSG_CHECKING(size of $1)
AC_CACHE_VAL(AC_CV_NAME,
[for ac_size in 4 8 1 2 16 $2 ; do # List sizes in rough order of prevalence.
  AC_TRY_COMPILE([#include "confdefs.h"
#include <sys/types.h>
$2
], [switch (0) case 0: case (sizeof ($1) == $ac_size):;], AC_CV_NAME=$ac_size)
  if test x$AC_CV_NAME != x ; then break; fi
done
])
if test x$AC_CV_NAME = x ; then
  AC_MSG_ERROR([cannot determine a size for $1])
fi
AC_MSG_RESULT($AC_CV_NAME)
AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [The number of bytes in type $1])
undefine([AC_TYPE_NAME])dnl
undefine([AC_CV_NAME])dnl
])