问题描述
我在网上搜索了很多,但是我找不到一个适用于g ++的例子,所有例子都使用gcc。
我收到的错误是:
wrap_malloc.o:函数`__wrap_malloc(unsigned int)':
wrap_malloc.cc:(.text+0x20):未定义引用`__real_malloc(unsigned int)'
wrap_malloc.o:在函数`main'中:
wrap_malloc.cc:(.text+0x37 ):未定义的引用`__wrap_malloc'
collect2:ld返回1退出状态
创建这个错误的代码如下(如果我使用gcc编译它,并将头文件从cstdio更改为stdio.h,则此代码有效):
#include< cstdio>
#include< cstdlib>
void * __ real_malloc(size_t);
$ b无效* __ wrap_malloc(size_t c){
printf(我的malloc调用%d \ n,c);
return __real_malloc(c);
}
int main(void){
void * ptr = malloc(12);
免费(ptr);
返回0;
}
这是我编译它的方式:
wrap_malloc.o:wrap_malloc.cc
g ++ -c wrap_malloc.cc -o wrap_malloc.o
wrap_malloc:wrap_malloc.o
g ++ wrap_malloc.o -o wrap_malloc -Wl,wrap,malloc
谢谢!
当您使用C ++编译器时,所有名称都会被损坏。当你运行 nm wrap_malloc.o
时,这意味着什么,它应该给你这样的东西:
<$ p $ 00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
U __Z13__real_mallocj
00000000 T __Z13__wrap_mallocj
U _printf
这意味着您使用( U )a符号称为 __ Z13__real_mallocj
并且您在文本段( T )中定义了一个名为 __ Z13__wrap_mallocj
。但是您可能需要一个名为 __ real_malloc
的符号。为了达到这个目的,你必须说编译器: __ real_malloc
是一个C风格的函数,像这样:
externCvoid * __ real_malloc(size_t);
externCvoid * __ wrap_malloc(size_t c){
printf(我的malloc调用%d \ n,c);
return __real_malloc(c);
现在输出 nm
是:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
U ___real_malloc
00000000 T ___wrap_malloc
U _printf
您可以看到名称 _printf
没有改变。这是因为在头文件中,许多函数已被声明为 externC
已经存在。
注意:I在cygwin环境中,在Windows上执行了上述所有操作。这就是为什么在外部符号中有一个额外的前导下划线。
I have searched online a lot but I couldn't find an example that works with g++, all examples work with gcc.The error I keep getting is
wrap_malloc.o: In function `__wrap_malloc(unsigned int)':
wrap_malloc.cc:(.text+0x20): undefined reference to `__real_malloc(unsigned int)'
wrap_malloc.o: In function `main':
wrap_malloc.cc:(.text+0x37): undefined reference to `__wrap_malloc'
collect2: ld returned 1 exit status
The code that creates this error is the following (this code works if I compile it with gcc and change the headers from cstdio to stdio.h):
#include <cstdio>
#include <cstdlib>
void *__real_malloc(size_t);
void *__wrap_malloc(size_t c) {
printf("My malloc called with %d\n", c);
return __real_malloc(c);
}
int main(void) {
void *ptr = malloc(12);
free(ptr);
return 0;
}
This is how I compile it:
wrap_malloc.o: wrap_malloc.cc
g++ -c wrap_malloc.cc -o wrap_malloc.o
wrap_malloc: wrap_malloc.o
g++ wrap_malloc.o -o wrap_malloc -Wl,--wrap,malloc
Thank you!
When you use a C++ compiler, all names are mangled. What this means becomes clear when you run nm wrap_malloc.o
, which should give you something like this:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
U __Z13__real_mallocj
00000000 T __Z13__wrap_mallocj
U _printf
This means that you use (U) a symbol called __Z13__real_mallocj
and that you define a symbol in the text segment (T) called __Z13__wrap_mallocj
. But you probably want a symbol called __real_malloc
. To achieve this you have to say the compiler that __real_malloc
is a C-style function, like this:
extern "C" void *__real_malloc(size_t);
extern "C" void *__wrap_malloc(size_t c) {
printf("My malloc called with %d\n", c);
return __real_malloc(c);
}
Now the output of nm
is:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
U ___real_malloc
00000000 T ___wrap_malloc
U _printf
You can see that the name _printf
hasn't changed. This is because in the header files, many functions are declared as extern "C"
already.
Note: I did all of the above on Windows in the cygwin environment. That's why there is an additional leading underscore in the external symbols.
这篇关于有没有人用C ++封装函数的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!