[英]Has anyone an example for wrapping a function in C++?
I have searched online a lot but I couldn't find an example that works with g+, all examples work with GCC. 我在网上搜索了很多,但我找不到一个与g +一起工作的例子,所有例子都适用于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): 创建此错误的代码如下(如果我使用GCC编译它并将标头从cstdio更改为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
When you use a C++ compiler, all names are mangled. 使用C ++编译器时,所有名称都会被破坏。 What this means becomes clear when you run
nm wrap_malloc.o
, which should give you something like this: 当你运行
nm wrap_malloc.o
,这意味着什么,这应该给你这样的东西:
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
. 这意味着你使用的(U)被称为符号
__Z13__real_mallocj
和您定义称为文本段(T)的象征__Z13__wrap_mallocj
。 But you probably want a symbol called __real_malloc
. 但是你可能想要一个名为
__real_malloc
的符号。 To achieve this you have to say the compiler that __real_malloc
is a C-style function, like this: 要实现这一点,你必须说编译器
__real_malloc
是一个C风格的函数,如下所示:
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: 现在
nm
的输出是:
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. 您可以看到名称
_printf
没有更改。 This is because in the header files, many functions are declared as extern "C"
already. 这是因为在头文件中,许多函数已经被声明为
extern "C"
。
Note: I did all of the above on Windows in the cygwin environment. 注意:我在cygwin环境中的Windows上完成了以上所有操作。 That's why there is an additional leading underscore in the external symbols.
这就是为什么外部符号中还有一个额外的前导下划线。
If this is the complete code, the problem you are having is that you haven't implemented __real_malloc()
! 如果这是完整的代码,那么您遇到的问题是您没有实现
__real_malloc()
!
And by the way, identifiers with double-underscores are reserved by the language. 顺便说一句,具有双下划线的标识符由语言保留。 You might want to think about picking different names.
您可能想考虑选择不同的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.