#includes 在 C 文件中用于处理器特定的实现
我正在开发专门为一种类型的嵌入式处理器编写的“C”代码库。我已经为 LED、GPIO 线和 ADC(使用结构体等)编写了通用的“伪面向对象”代码。我还编写了大量代码,以与硬件/目标无关的方式利用这些“对象”。
我们现在正在混合使用另一种处理器类型,我想保留当前的代码结构,这样我仍然可以使用更高级别的库。然而,我确实需要为较低级别的代码(LED、GPIO、ADC)提供不同的实现。
我知道 .C 文件中的 #include 通常会被轻视,但在这种情况下,它合适吗?例如:
// led.c
#ifdef TARGET_AVR
#include "led_avr.c"
#elseifdef TARGET_PIC
#include "led_pic.c"
#else
#error "Unspecified Target"
#endif
如果这不合适,更好的实现是什么?
谢谢!
I'm working on a 'C' code base that was written specifically for one type of embedded processor. I've written generic 'psuedo object-oriented' code for things like LEDs, GPIO lines and ADCs (using structs, etc). I have also written a large amount of code that utilizes these 'objects' in a hardware/target agnostic manner.
We are now tossing another processor type into the mix, and I'd like to keep the current code structure so I can still make use of the higher level libraries. I do, however, need to provide different implementations for the lower level code (LEDs, GPIO, ADCs).
I know #includes in .C files are generally looked down upon, but in this case, is it appropriate? For example:
// 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 a better implementation?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于链接器不关心源文件的实际名称是什么(它只关心导出的符号),因此您可以更改每个目标的链接器命令行以命名适当的实现模块(
led_avr.c 或
led_pic.c
)。管理多个平台源文件的常见方法是将每组平台实现文件放在自己的目录中,因此您可能有
avr/led.c
和pic/led.c(以及
avr/gpio.c
和pic/gpio.c
等)。Since the linker doesn't care what the name of a source file actually is (it only cares about exported symbols), you can change your linker command line for each target to name the appropriate implementation module (
led_avr.c
orled_pic.c
).A common way to manage multiple platform source files is to put each set of platform implementation files in their own directory, so you might have
avr/led.c
andpic/led.c
(andavr/gpio.c
andpic/gpio.c
, etc).这很好。您可以使用其他技巧,例如:
It is good. You may use other tricks, like:
更常见的方法是更改构建系统(无论它是什么)来编译或不编译那些某些 C 文件,而不是包含 C 文件。
The more common way to do that, instead of including a C file, is to change the build system (whatever it is) to compile or not compile those certain C files.