Replacing constants #defines

I am currently working on a project (in C), and we use #defines for the default variables. These variables are used to build the structure of a buffered battery.

Now we have different customers who have different layouts. In the past, we would simply create a whole new piece of software.
Now we are trying to get away from this fragmentation and use the same code base.

We have a shared library (used in all projects) that has a set of #define and declares structures. We currently have a custom header file (which contains #defines for this project) and it compares them with #define in a shared library. If it is different, it throws an error.

We want to save a custom header file, but I'm trying to find a solution to replace #define in a shared library. One idea is to replace them with some variable, the other is to use a certain type of preprocessor.

In the past, we used ifdef , but the code is really cluttered and terrible, so we are also trying to get away from it.

Anyone have any solutions?

+4
source share
2 answers

You can have everything #define enclosed in brackets on

 #ifndef HUI # define HUI 1023 #endif 

and then put the ones you want to change for individual compilation on the compilation command line with -DHUI=1033 .

+4
source

Quick fix: Put the definitions in separate header files. in the main file include header files via #fidef

This will work if the number of different headers is not greater than handfull.

Another solution: split header files as described above, but instead of selecting inclusions via ifdef, place the definitions in separate folders and make a separate make file, whether the folders were added to the include path. -> keeps the code clean, but now you have #customer makefiles

+2
source

Source: https://habr.com/ru/post/1343191/


All Articles