是否可以用我自己的覆盖 <string.h> 中的 strdup?
is it possible to overwrite strdup from <string.h> with my own?
我需要这样做,因为我应该为单元测试存根这个函数。
我使用的是 gcc 5.4.0 版。
对于 glibc 中存在的许多其他函数,比如 memcpy,我只是在我的 .c 文件中编写了实现,然后使用这个实现来代替原来的实现,但是对于 strdup 和其他一些函数,我得到这样的编译错误:
error: expected identifier or ‘(’ before ‘__extension__’
char* strdup (const char *__s)
我知道这是因为我正在尝试使用 string.h 中已经存在的函数名称,尽管它也可以与它们中的其他一些函数一起使用。我想知道是否有可能以某种方式绕过它。
在你的例子中,原来的 strdup
是一个预处理器宏,而不是一个实际的函数。您可以尝试摆脱它:
#undef strdup
char *strdup(const char *s) {
// your code goes here
}
我需要这样做,因为我应该为单元测试存根这个函数。
我使用的是 gcc 5.4.0 版。
对于 glibc 中存在的许多其他函数,比如 memcpy,我只是在我的 .c 文件中编写了实现,然后使用这个实现来代替原来的实现,但是对于 strdup 和其他一些函数,我得到这样的编译错误:
error: expected identifier or ‘(’ before ‘__extension__’
char* strdup (const char *__s)
我知道这是因为我正在尝试使用 string.h 中已经存在的函数名称,尽管它也可以与它们中的其他一些函数一起使用。我想知道是否有可能以某种方式绕过它。
在你的例子中,原来的 strdup
是一个预处理器宏,而不是一个实际的函数。您可以尝试摆脱它:
#undef strdup
char *strdup(const char *s) {
// your code goes here
}