用于选择性替换的 C 宏技巧

标签 c macros c-preprocessor

是否有一个宏技巧可以在不影响函数定义的情况下只重命名函数调用,特别是对于 gcc/cpp:

#define get_resolution __mock_get_resolution

上面的宏改变了所有地方,但我只是想让这个对函数调用get_resolution();生效,而不影响定义void get_resolution()

void get_resolution()
{
}

void display()
{
    get_resolution();
}

最佳答案

作为特定于 gcc 的解决方案,

 The `alias' attribute causes the declaration to be emitted as an
 alias for another symbol, which must be specified.  For instance,

      void __f () { /* Do something. */; }
      void f () __attribute__ ((weak, alias ("__f")));

关于用于选择性替换的 C 宏技巧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11762529/

相关文章:

c - 我对 K&R 练习 1-22 的解决方案有什么问题?

c - 如何使用 "scanf"输入特定字母?

emacs - pretty-print 和扩展

objective-c - : Use of undeclared identifier '_cmd' 的解决方法

c++ - FOR_EACH 宏在调用宏中有两个或多个参数

c - typedef 和 #define 同名

c - 如何将字符串拆分为字符串数组?

scala - 如何用其他树替换子树?

c - 如何 "#define"结构体字段或结构体名称

c - 将宏定义为行号