[ad_1]
I want to call my custom malloc/new calls instead of standard library functions.
1. for malloc, I have this short program
#include <stdio.h>
#include <stdlib.h>
void *__real_malloc(size_t size);
void *__wrap_malloc(size_t size)
{
void *ptr = __real_malloc(size);
printf("malloc(%ld) = %p\n", size, ptr);
return ptr;
}
int main(void) {
int* ptr = (int*) malloc(sizeof(int));
return 0;
}
I use the following command to compile
g++ -Wl,--wrap,malloc main.cpp
But getting below error
/usr/bin/ld: /tmp/ccnB04KY.o: in function `__wrap_malloc(unsigned long)':
b3.cpp:(.text+0x18): undefined reference to `__real_malloc(unsigned long)'
/usr/bin/ld: /tmp/ccnB04KY.o: in function `main':
b3.cpp:(.text+0x54): undefined reference to `__wrap_malloc'
collect2: error: ld returned 1 exit status
This works with gcc for .c files but not with g++ and .cpp files. What’s the issue?
2. Also I cant figure out how to overide new calls?
[ad_2]