Home > front end >  Is it possible to use the preprocessor to create stubs from a header file?
Is it possible to use the preprocessor to create stubs from a header file?

Time:02-03

When I am writing unit tests in C , often I need to create some stub method definitions of free functions which are declared in a header file actions.h:

#ifdef __cplusplus
extern "C" {
#endif
void action1(void);
void action2(void);
int action3(void);
#ifdef __cplusplus
}
#endif

Instead of creating the stubs in a separate file

void action1() {}
void action2() {}
int action3() { return 0; }

it would be convenient to define the stubs on the fly in the test code when including this header:

create_stubs(#include <actions.h>)

CodePudding user response:

Is it possible to use the preprocessor to create stubs from a header file?

No, it is not possible.

Use another language to parse the header and generate the source file. Python, Perl, AWK or similar.

CodePudding user response:

C preprocessor doesn't parse C code, it is a wrong tool for your task.

You may like to use your compiler to parse the header files and generate an Abstract Syntax Tree, using which you can find function declarations and generate stub implementations.

  •  Tags:  
  • Related