I am working on a cross-platform project that uses GNU Autotools for the build system, and I was wondering if there is a way to run a simple sizeof(int) and pass the result to the configure script (or even simply to the standard output) when cross-compiling.
Let's imagine I am using a 32-bit machine for compiling a program for a 64-bit machine. If my compiler is able to compile code for a different architecture it means that surely knows the result of sizeof(int)on the target architecture.
My question is: How do I interrogate the compiler for obtaining that information when cross-compiling?
EDIT
As Ian Abbott has explained in the comments, there is a AX_COMPILE_CHECK_SIZEOF() macro in the Autoconf Archive able to compute sizeof()via compile checks (not run checks) that therefore works when cross-compiling. Unfortunately the macro is not able to deal with expressions that contain non-alphanumeric characters (like, for example, sizeof(sizeof(char)), where sizeof(char) contains round brackets), so I have published a fork of it that optionally allows to give alphanumeric “labels” to the checks, to be used as valid variable names:
dnl NA_SANITIZE_VARNAME(string)
dnl **************************************************************************
dnl
dnl Replaces `/\W/g,` with `'_'` and `/^\d/` with `_\0`
dnl
dnl From: not-autotools/m4/not-autotools.m4
dnl
dnl **************************************************************************
AC_DEFUN([NA_SANITIZE_VARNAME],
[m4_if(m4_bregexp(m4_normalize([$1]), [[0-9]]), [0], [_])[]m4_translit(m4_normalize([$1]),
[ !"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~],
[__________________________________])])
dnl NC_CC_CHECK_SIZEOF(data-type[, headers[, store-as[, extra-sizes]]])
dnl **************************************************************************
dnl
dnl Checks for the size of `data-type` using **compile checks**, not run
dnl checks.
dnl
dnl From: https://github.com/madmurphy/not-autotools/blob/master/m4/not-cc.m4
dnl
dnl **************************************************************************
AC_DEFUN([NC_CC_CHECK_SIZEOF], [
m4_pushdef([__label__],
NA_SANITIZE_VARNAME([sizeof_]m4_tolower(m4_ifblank([$3],
[[$1]], [[$3]]))))
AC_MSG_CHECKING([size of `$1`])
AC_CACHE_VAL([ac_cv_]__label__, [
# List sizes in rough order of prevalence.
for nc_sizeof in 4 8 1 2 16 m4_normalize([$4]) ; do
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([[$2]], [[
switch (0) {
case 0:
case (sizeof ($1) ==
${nc_sizeof}):;
}
]])
],
[AS_VAR_COPY([ac_cv_]__label__, [nc_sizeof])])
AS_IF([test "x${ac_cv_]__label__[}" != x], [break;])
done
])
AS_IF([test "x${ac_cv_]__label__[}" = x], [
AC_MSG_RESULT([??])
AC_MSG_ERROR([cannot determine a size for $1])
])
AC_MSG_RESULT([${ac_cv_]__label__[}])
AC_DEFINE_UNQUOTED(m4_toupper(m4_quote(__label__)),
[${ac_cv_]__label__[}],
[The number of bytes in type $1])
m4_ifnblank([$3],
[AS_VAR_COPY([na_]m4_quote(m4_tolower([$3])), [nc_sizeof])])
m4_popdef([__label__])
])
With this version we can use, for example, a size_t label for the expression sizeof(sizeof(char)):
NC_CC_CHECK_SIZEOF([sizeof(char)], [], [size_t])
By doing so, NC_CC_CHECK_SIZEOF() will export a shell variable named ${ac_cv_sizeof_size_t} within the configure script and a preprocessor macro named SIZEOF_SIZE_T within the C environment, both containing the expansion of sizeof(sizeof(char)).
Autoconf's native AC_CHECK_SIZEOF() macro
As Brett has pointed out in the comments, there is a native AC_CHECK_SIZEOF() macro that apparently doesn't seem to limit the computed size to the powers of 2 (plus the manually-provided sizes), as AX_COMPILE_CHECK_SIZEOF() does. Unfortunately, after after a quick check, I have found out that also AC_CHECK_SIZEOF() does not allow to pass sizeof(char) as argument (if I try, I get error: AC_CHECK_SIZEOF: requires literal arguments– the same happens if I use Autoconf's quadrigraphs by writing AC_CHECK_SIZEOF([sizeof@{:@char@:}@])).
Appendix – computing CHAR_BIT when cross-compiling
This is slightly off-topic, but since I needed it, Autoconf Archive's AX_COMPILE_CHECK_SIZEOF() macro inspired me to write a M4 macro that computes CHAR_BIT when cross-compiling (the macro does not require limits.h for working):
dnl NC_CC_CHECK_CHAR_BIT
dnl **************************************************************************
dnl
dnl Calculates the size in bits of the `char` data type using compile checks
dnl
dnl From: https://github.com/madmurphy/not-autotools/blob/master/m4/not-cc.m4
dnl
dnl **************************************************************************
AC_DEFUN([NC_CC_CHECK_CHAR_BIT], [
AC_MSG_CHECKING([size of `char` in bits])
AC_CACHE_VAL([ac_cv_char_bit], [
# Minimum size in bits for `char` is guaranteed to be 8
for nc_char_bit in {8..64}; do
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM(, [[
switch (0) {
case 0: case ((unsigned char)
(1 << ${nc_char_bit})):;
}
]])
], [], [break])
done
AS_VAR_COPY([ac_cv_char_bit], [nc_char_bit])
])
AC_MSG_RESULT([${ac_cv_char_bit}])
AC_DEFINE_UNQUOTED([COMPUTED_CHAR_BIT],
[${ac_cv_char_bit}],
[The number of bits in `char`])
])
After calling NC_CC_CHECK_CHAR_BIT (without arguments), a shell variable named ${ac_cv_char_bit} is made available within the configure script and a preprocessor macro named COMPUTED_CHAR_BIT is made available within the C environment, both containing the size of the char data type measured in bits.