编译这个工程 的时候,遇到了这样的错误:
$ make
make all-am
make[1]: Entering directory `/home/tiina/clearwater/sprout/modules/libmemcached'
CXX libhashkit/libhashkit_libhashkit_la-aes.lo
In file included from /opt/rh/devtoolset-9/root/usr/include/c++/9/stdlib.h:36,
from ./libhashkit/common.h:45,
from libhashkit/aes.cc:38:
/opt/rh/devtoolset-9/root/usr/include/c++/9/cstdlib:151:11: error: '::malloc' has not been declared
151 | using ::malloc;
| ^~~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/cstdlib:164:11: error: '::realloc' has not been declared
164 | using ::realloc;
| ^~~~~~~
In file included from ./libhashkit/common.h:45,
from libhashkit/aes.cc:38:
/opt/rh/devtoolset-9/root/usr/include/c++/9/stdlib.h:65:12: error: 'std::malloc' has not been declared
65 | using std::malloc;
| ^~~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/stdlib.h:73:12: error: 'std::realloc' has not been declared
73 | using std::realloc;
| ^~~~~~~
make[1]: *** [libhashkit/libhashkit_libhashkit_la-aes.lo] Error 1
make[1]: Leaving directory `/home/tiina/clearwater/sprout/modules/libmemcached'
难道是C++标准库出了问题? NO 问题出在configure.ac文件里的 AC_FUNC_MALLOC configure.ac里的这句话,会在config.h文件里增加一句: #define malloc rpl_malloc 而/opt/rh/devtoolset-9/root/usr/include/c++/9/cstdlib 里面又有#undef malloc 所以程序运行起来类似这样:
#define malloc rpl_malloc
#include <stdlib.h>
#undef malloc
namespace std {
using ::malloc;
}
// Your program:
int main()
{
int* i = static_cast(std::malloc(sizeof(int)));
return 0;
}
( 虽然上文是应用中写的,但实际上#undef malloc 是在stdlib.h的第135行通过#include<cstdlib> ,从cstdlib的头文件里引入的)。 使用g++ -P -E test.cc 可以看到:
468 extern void *rpl_malloc (size_t __size) throw () __attribute__ ((__malloc__)) ;
因此,报的错误也是在cstdlib的using ::malloc 和using std::malloc )
configure.ac的AC_FUNC_MALLOC
autoconf工具会在运行过程中执行一系列的test,其中就有malloc(0)是否返回NULL,如果是的话就认为是用的rpl_malloc——一个需要你自己定义的malloc。但是会让这个test返回NULL的可能性太多了,比如lib库的路径写错了,它只会有提示,而不是终止编译,这种虽和malloc无关,但也会返回NULL。为了不执行这种test,把configure.ac的AC_FUNC_MALLOC去掉,就能通过编译了。
感谢: https://nerdland.net/unstumping-the-internet/malloc-has-not-been-declared/#rootcause
|