I have a mem_malloc() and mem_free() defined for me and I want to use them to replace the malloc() and free() and consequently C++'s new and delete.
I define them as follows:
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);
}
}
However, I get two link errors:
[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 reference to `dlopen'
mem_debug.c:(.text+0x89d): undefined reference to `dlsym'
mem_debug.c:(.text+0xa03): undefined reference to `dlclose'
mem_debug.c:(.text+0xa24): undefined reference to `dlclose'
mem_debug.c:(.text+0xa2e): undefined reference to `dlerror'
collect2: ld returned 1 exit status
1) How do I get the multiply defined malloc() and free() errors to go away and just take my definition and not the built in one?
2) What library provides dlopen() and friends? I'd expect this to be built in, but they are undefined.