#includes in C files for processor-specific implementations

I am working on a C code base written specifically for one type of embedded processor. I wrote a generic “object oriented” psuedo code for things like LEDs, GPIO lines, and ADCs (using structures, etc.). I also wrote a large amount of code that uses these “objects” in agnostic / hardware mode.

Now we are moving a different type of processor to the mix, and I would like to keep the current code structure, so I can still use higher-level libraries. However, I need to provide various implementations of the lower level code (LEDs, GPIO, ADC).

I know that #includes in .C files usually looks down, but in this case, is this appropriate? For instance:

// led.c
#ifdef TARGET_AVR
#include "led_avr.c"
#elseifdef TARGET_PIC
#include "led_pic.c"
#else
#error "Unspecified Target"
#endif

If this is inappropriate, what is better for implementation?

Thank!

+3
source share
3 answers

Since the linker doesn't care what the name of the source file actually is (it only cares about exported characters), you can change your linker command line for each target to name the corresponding implementation module ( led_avr.cor led_pic.c).

A common way to manage multiple source files of the platform is the location of each set of platform implementation files in their own directory, so you can be avr/led.cand pic/led.c(and avr/gpio.cand pic/gpio.cetc.).

+3
source

It's good. You can use other tricks, for example:

#ifdef PROC1
#define MULTI_CPU(a,b) (a)
#else
#define MULTI_CPU(a,b) (b)
#endif
+2

, , C, - ( ) C.

0

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


All Articles