Created
July 30, 2025 10:49
-
-
Save hidsh/acd72842fd29c7ec18e3eb01c4594b65 to your computer and use it in GitHub Desktop.
c example to override functions using `#define` macros for inline functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int fun0(void) { return 5; } | |
int fun1(int x) { return x * 10; } | |
// inline int fun1_inline(int x) { return x * 100; } // => undefined reference | |
extern inline int fun1_inline_extern(int x) { return x * 100; } | |
static inline int fun1_inline_static(int x) { return x * 1000; } | |
#define OVERRIDE 1 | |
#if OVERRIDE == 1 | |
#define fun0() (9) | |
#define fun1(x) (99) | |
#define fun1_inline_extern(x) (999) | |
#define fun1_inline_static(x) (9999) | |
// To override functions, these `#define funXX` macros | |
// must be placed *after* the function definitions overridden | |
// regardless of thier prototype declarations or not. | |
#endif | |
int main(void) | |
{ | |
printf("OVERRIDE = %d: fun0 => %d, fun1 => %d\n", | |
OVERRIDE, fun0(), fun1(2)); | |
printf("OVERRIDE = %d: fun1_inline_extern => %d, fun1_inline_static => %d\n", | |
OVERRIDE, fun1_inline_extern(2), fun1_inline_static(2)); | |
return 0; | |
} | |
/* | |
OVERRIDE = 0: fun0 => 5, fun1 => 20 | |
OVERRIDE = 0: fun1_inline_extern => 200, fun1_inline_static => 2000 | |
OVERRIDE = 1: fun0 => 9, fun1 => 99 | |
OVERRIDE = 1: fun1_inline_extern => 999, fun1_inline_static => 9999 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment