I am working on a C project where I'm defining an API, and then have multiple different implementations of that API, something like the below:
|-api.h
|-implementation_1.c
|-implementation_2.c
I'm working with CMake, so currently to select which implementation is used by the application, the user has to set a define, e.g. -DIMPLEMENTATION_SEL_1
and also select which of the implemmentation_x.c
files are included in the build.
I was thinking it could be nice to simplify the selection to depend only on the define, by adding an extra source file with #includes to the implementation-specific source files, something like this...
|-api.h
|-implementation.c
|-implementation_1.c
|-implementation_2.c
//implementation.c
#if defined IMPLEMENTATION_SEL_1
#include "implementation_1.c"
#elif defined IMPLEMENTATION_SEL_2
#include "implementation_2.c"
#else
#error "Invalid API implementation selected"
#endif
This way my CMake file only ever has to specify implementation.c
as part of the build, and only the define should change to select between implementations.
My question is: is there any reason I should avoid this approach? I've heard it's bad practice to include source files, but to me it seems in this case it might be cleaner than the alternatives