如何在c/c++中设置与函数同名的定义?

How to set defines with the same name as a function in c /c++?

我有一个没有很好记录的 c 库,它允许我覆盖一些函数。但遗憾的是没有例子:(

library.h:

#ifndef some_function
uint8_t some_function(void);
#endif

所以我在我的 C++ 代码中定义了这样一个函数:

#include "library.h"

extern "C" uint8_t some_function(void) { return 0;}

void main() {
....
}

但它使用库中定义的代码。

下次尝试:

#define some_function
#include "library.h"

uint8_t some_function(void) { return 0;}

结果:

src/main.cpp: error: expected unqualified-id before 'void'
 extern "C" uint8_t some_function(void);

因为函数名被定义替换了。

有什么建议吗?

Any suggestions?

#define some_function some_function

当然不提倡这样做,但它应该有效。

可能是作者想让用户用不同的名字定义自己的函数。 IE。宏对您来说意义不大,因为库使用自定义函数名称。

uint8_t my_function(void) { return 0;}

...

#define some_function my_function
#include "library.h"

如果没有人定义这样的宏,作者定义了一个具有相同功能的函数,因此所有调用都可以工作。