简体   繁体   English

如何取消定义库函数以使用我们的相同函数版本

[英]How to undef a library function to use our version of same function

How to undef a library function to use my version of same function.如何取消定义库函数以使用我的相同函数版本。 Notice that I need to include the header file for other functions of same file.请注意,我需要为同一文件的其他函数包含头文件。 So not including is it not an option.所以不包括它不是一种选择。 Is there any way to use it without changing the name?有什么办法不用改名字就可以使用吗?

You can do the following:您可以执行以下操作:

#define function_name function_name_orig
#include <library.h>
#undef function_name

int function_name() {
    /* ... */
}

This way the function will not be defined by the header, since it will be replaced by function_name_orig .这样函数就不会被头文件定义,因为它将被function_name_orig替换。 Implementations of getters or setters in the header file may continue to work - even if they use function_name , since those calls will also be replaced.头文件中 getter 或 setter 的实现可能会继续工作 - 即使它们使用function_name ,因为这些调用也将被替换。

I just ran across this, and I couldn't do @urzeit's answer because it was std::sqrt.我刚刚遇到了这个,我无法回答 @urzeit 的答案,因为它是 std::sqrt。 The compile error was ambiguity.编译错误是模棱两可的。 But, extending @urseit's answer, I managed this solution:但是,扩展@urseit 的答案,我管理了这个解决方案:

// These fix occurrences of 'sqrt' on its own:
inline float stdSqrt(float val) { return ::std::sqrt(val); }
inline double stdSqrt(double val) { return ::std::sqrt(val); }
// These fix occurrences of scoped 'std::sqrt':
namespace std {
inline float stdSqrt(float val) { return ::std::sqrt(val); }
inline double stdSqrt(double val) { return ::std::sqrt(val); }
}
// Now we can apply urzeit's answer
#define sqrt(x) stdSqrt(x)r
#include "otherLibraryHeader.h"
#undef sqrt

For gcc, #undef seems to cut it, so long as you're keeping the same prototype for the function.对于 gcc,#undef 似乎可以削减它,只要您为函数保留相同的原型。 For example:例如:

#include <stdio.h>
#undef scanf

int scanf(const char * s, ...)
{
    printf(s);
    return 0;
}

int main()
{
    scanf("hello\n");
    return 0;
}

This compiles without warnings with -Wall, but if you want scanf to have a prototype of (say) void scanf(void) it will give errors.使用 -Wall 编译时不会发出警告,但是如果您希望 scanf 具有(例如) void scanf(void)的原型,它将给出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中向标准库添加我们自己的函数 - How to add our own function to standard library in C++ 是否有必要在 function 中取消定义宏? - Is it necessary to undef macros within function? 在其他库中存在相同功能的情况下,如何要求库使用其内部功能 - How to ask library to use its internal function in case same function exist in other libs 如何使用在同一个包装函数中使用malloc的函数预加载共享库并将malloc包装在一起? - How to preload shared library and wrap malloc alongside using functions which use malloc in the same wrapped function? 我们可以在 2 个不同的库文件中使用相同的 function,如果两个库都在同一个 DLL 文件中使用 - Can we use same function in 2 different library file , if both the library consumed in same DLL file #ifdef,#ifndef和#undef是否可以与类似函数的宏一起使用? - Do #ifdef, #ifndef and #undef work with function-like macros? 如何在汇编语言中使用c库函数fgets? - How to use c library function fgets in assembly language? 如何使用外部库中的函数? - How do I use function from external library? 如何在gcc中使用英特尔的Math函数库? - How to use Intel's Math function library with gcc? 共享库功能未在相同的内存位置加载 - Shared library function not loaded at same memory location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM