Home:ALL Converter>Switching between implementations in C precompiler

Switching between implementations in C precompiler

Ask Time:2017-03-18T06:30:59         Author:Ward Beullens

Json Formatter

I'm fairly new to programming in C. My problem is that I have two implementations of a function and I want to be able to switch between them easily.

Right now I define the two implementations of the function as function_implementation1 and function_implementation1 in the files "funtion_implementation1.h" and "funtion_implementation2.h" respectively. To switch between them I have the following file:

#define IMPLEMENTATION1

#ifdef IMPLEMENTATION_1
    #include "funtion_implementation1.h"
    #define myFunction function_implementation1
#endif

#ifdef IMPLEMENTATION_2
    #include "funtion_implementation2.h"
    #define myFunction function_implementation2
#endif

In order to switch from one implementation to the other I just have to change the first line. This approach works, and I was satisfied with it for a while, but now it is bugging me that I have to open this file so often. I have a parameters.h file where I define all my parameters and I would rather choose which implementation to use in that file. Sadly, moving the first line to that file does not work. If I do that myFunction is not defined.

What is the best way to do this?

Author:Ward Beullens,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42868072/switching-between-implementations-in-c-precompiler
SMFSW :

you should include your parameters file where you use alias, macros, etc:\n\n#include \"Parameters.h\"\n\n\nalso, all your headers files should start with:\n\n#ifndef __FILE_H__\n #define __FILE_H__\n\n// definitions go there\n\n#endif\n\n\nThis prevents nested include of header files",
2017-03-17T22:34:52
yy