简体   繁体   中英

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

I have a not very well documented c library which allows me to override some functions. But sadly there are no examples :(

library.h:

#ifndef some_function
uint8_t some_function(void);
#endif

So I defined a function like this in my c++ code:

#include "library.h"

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

void main() {
....
}

but it uses the code define in the library.

Next try:

#define some_function
#include "library.h"

uint8_t some_function(void) { return 0;}

results in:

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

because function name is replaced by the define.

Any suggestions?

Any suggestions?

#define some_function some_function

Not advocating for this, of course, but it should work.

Maybe the author wanted to allow the users to define their own functions with different names. Ie the macro is not meant as much for you, as for the library to use a custom function name.

uint8_t my_function(void) { return 0;}

...

#define some_function my_function
#include "library.h"

If no one defined such macro, the author defined a function with the same so all the calls would work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM