相关疑难解决方法(0)

如何在 Linux 中重新定义 malloc() 以在 C++ new 中使用

我为我定义了 mem_malloc() 和 mem_free(),我想用它们来替换 malloc() 和 free() 以及 C++ 的 new 和 delete。

我将它们定义如下:

extern "C" {

extern void *mem_malloc(size_t);
extern void mem_free(void *);

void *
malloc(size_t size) {
  return mem_malloc(size);
}

void
free(void *memory) {
  mem_free(memory);
}
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到两个链接错误:

[user@machine test]$ g++ -m32 -pthread main.cpp -static  libmemnmf-O.a
/usr/lib/../lib/libc.a(malloc.o): In function `free':
(.text+0x153c): multiple definition of `free'
/tmp/ccD2Mgln.o:main.cpp:(.text+0x842): first defined here
/usr/lib/../lib/libc.a(malloc.o): In function `malloc':
(.text+0x3084): multiple definition of `malloc'
/tmp/ccD2Mgln.o:main.cpp:(.text+0x856): first defined here
libmemnmf-O.a(mem_debug.o): In function `mem_init_debug_routines':
mem_debug.c:(.text+0x83c): undefined …
Run Code Online (Sandbox Code Playgroud)

c++ malloc redefine linker-errors

4
推荐指数
1
解决办法
4998
查看次数

标签 统计

c++ ×1

linker-errors ×1

malloc ×1

redefine ×1