[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to test HAVE_MALLOC
From: |
Raphael 'kena' Poss |
Subject: |
Re: how to test HAVE_MALLOC |
Date: |
Fri, 5 Feb 2010 21:08:02 +0100 |
Hi Joachim,
Op 5 feb 2010, om 16:55 heeft j.wuttke het volgende geschreven:
> A user of my software package reported that compilation on
> a virtual Linux machine running on his Mac failed because
> '::malloc' was not declared.
I have encountered the same issue as a producer of another software package. I
have found the issue is a compound effect of the following three things:
1) in a strictly conforming C++ compiler, when using the "cXXXX" form of the C
library headers all the C library declarations are put in the namespace "std",
and not the root namespace. When using the plain C header name, the std
namespace is not used.
2) you really want to #include <cstdlib> / <stdlib.h> *explicitly* to reach
malloc and not rely for its declaration to becomes visible magically through
some other include;
3) if your software code is C++ and you want to allow users to use a strictly
conforming compiler, then you want to be careful about the way malloc is
checked in your configure script. In particular the check for AC_FUNC_MALLOC
cannot be run in C++ mode.
To fix the issue the following works for me:
A) in configure.ac, check that malloc is reachable from C++, this way and not
with AC_FUNC_MALLOC:
AC_PROG_CXX
AC_LANG_PUSH([C++])
AC_MSG_CHECKING([whether <cstdlib> exposes std::malloc()])
AC_RUN_IFELSE(AC_LANG_PROGRAM(address@hidden:@include <cstdlib>], [return
!std::malloc(10)]),
[cxx_have_std_malloc=yes], [cxx_have_std_malloc=no])
AC_MSG_RESULT([$cxx_have_std_malloc])
AC_MSG_CHECKING([whether <cstdlib> exposes ::malloc()])
AC_RUN_IFELSE(AC_LANG_PROGRAM(address@hidden:@include <cstdlib>], [return
!::malloc(10)]),
[cxx_have_root_malloc=yes], [cxx_have_root_malloc=no])
AC_MSG_RESULT([$cxx_have_root_malloc])
AC_LANG_POP([C++])
if test $cxx_have_std_malloc = no && test $cxx_have_root_malloc = no; then
AC_MSG_ERROR([can't use malloc in C++])
else
AC_DEFINE([HAVE_MALLOC], 1)
if test $cxx_have_root_malloc = yes; then
AC_DEFINE([HAVE_MALLOC_IN_ROOT_NAMESPACE], 1)
fi
fi
B) in your source code, be careful to #include <cstdlib> where appropriate;
C) if HAVE_MALLOC_IN_ROOT_NAMESPACE is 0 or not set (as per the check above),
be sure that the following appears early where needed in the root namespace
before boost headers are included, and after <cstdlib> is included:
using std::malloc;
If you run into more issues I suggest you ask your customer / user to report
the results of config.log and the compilation error message after applying the
techniques above, you should then have more information available to
troubleshoot further.
Cheers,
--
k