前言
在项目中,我们经常会需要针对不同的需求进行不同的配置。
在windows/linux等大平台下,可能会用到配置文件 ini、xml等。而在嵌入式平台下,可能连文件系统都没有。而且很多时候我们只需要硬编码这些配置进代码里就好,不需要在运行时更改。
比如每台设备的设备信息等,在整个生命周期中是不会变的。所以并不需要用那么灵活的配置文件。
下面我就带大家游览一下c语言的宏配置相关技术,其可以实现灵活的代码裁剪定制。基于自己目前的积累,可能有错误或者遗漏,敬请指出。
故事
假设我们在开发一个设备的项目,简单起见,我们只写出其中一小部分。主函数就长这样就好了:
main.c:
#include device.hint main(){ device_printfmsg(); return 0;}
设备的方法简单起见就一个函数,打印自身信息:
device.h:
#ifndef _device_h#define _device_hvoid device_printfmsg(void);#endif
device.c:
#include device.h#include #include static const char *devtype = abs;static uint32_t devid = 34;void device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s_%u.local , devtype, devid);}
这样一个简单的设备就完成了:
但这样实在偶合太严重了。要是现在我多了一台设备,需要多维护一个设备,那最朴实的人肯定就屁颠屁颠的一个个去修改值了。要是偶尔修改一下,而且就几个参数还好,但实际中经常会有多个参数,而且会经常要修改,那直接人工修改就很不靠谱了。
而我第一反应可能会这么搞。
device.c:
#include device.h#include #include #if 0 static const char *devtype = abs; static uint32_t devid = 34;#else static const char *devtype = cba; static uint32_t devid = 33435;#endifvoid device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s_%u.local , devtype, devid);}
这是快速切换技术,这样我只要修改#if后面为1或0就能快速切换不同配置:
观察代码发现,冗余的代码有点多,而且比如那个domainname。很可能代码其他地方还会经常用到,这样把它的格式放在printf的格式字符串里就很不合适了,我们需要单独为它分配个字符串。
于是整理之后就变成了这样。
#include device.h#include #include #if 0 #define dev_name abs #define dev_id 34#else #define dev_name cba #define dev_id 33435#endif#define _str(s) #s#define mollocdefinetostr(mal) _str(mal)static const char devtype[] = mollocdefinetostr(dev_name);static uint32_t devid = dev_id;static const char devdname[] = mollocdefinetostr(dev_name) _ mollocdefinetostr(dev_id) .local;void device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);}
不用看了,运行结果和上面那个一模一样。
#define 就是宏定义,都在看宏配置技巧了应该其实是不需要解释宏在干什么了。
但是要强调的是,宏的作用是文本替换,注意是文本,预处理器并不认得变量不变量的,它只知道见到之前定义过的宏,就直接替换文本。
所以:
static uint32_t devid = dev_id;
这句其实经过预处理后就是:
static uint32_t devid = 33435;
我们看到其中mollocdefinetostr这个宏很有意思,这对宏是用于把宏展开后的值作为字符串的。
预处理后,
static const char devdname[] = mollocdefinetostr(dev_name) _ mollocdefinetostr(dev_id) .local;
这句就会变成:
static const char devdname[] = cba _ 33435 .local;
然后由于c语言里连续的字符串不分割的话会自动合并,上面这就相当于
static const char devdname[] = cba_33435.local;
接下来又来了一台设备。我忍,扩充下快速切换,弄成多路分支的那种。
#include device.h#include #include #define dev_abs 1#define dev_cba 2#define dev_lol 3// 选择当前的设备#define dev_select dev_lol#if (dev_select == dev_abs) #define dev_name abs #define dev_id 34#elif(dev_select == dev_cba) #define dev_name cba #define dev_id 33435#elif(dev_select == dev_lol) #define dev_name lol #define dev_id 1234#else #error please select current device by dev_select#endif#define _str(s) #s#define mollocdefinetostr(mal) _str(mal)static const char devtype[] = mollocdefinetostr(dev_name);static uint32_t devid = dev_id;static const char devdname[] = mollocdefinetostr(dev_name) _ mollocdefinetostr(dev_id) .local;void device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);}
这样每次这样在 #define dev_select 那修改一下对应的设备就好了,其实可读性还不错。
那句#error确保了你不会遗忘去配置它,因为如果你配置了个错误的值,预处理器会直接报错。
这时候,一般来说我会把配置相关的移到头文件中,就变成了这样:
device.h:
#ifndef _device_h#define _device_h#define dev_abs 1#define dev_cba 2#define dev_lol 3#ifndef dev_select #define dev_select dev_abs#endif#if (dev_select == dev_abs) #define dev_name abs #define dev_id 34#elif(dev_select == dev_cba) #define dev_name cba #define dev_id 33435#elif(dev_select == dev_lol) #define dev_name lol #define dev_id 1234#else #error please select current device by dev_select#endifvoid device_printfmsg(void);#endif
device.c:
#include device.h#include #include #define _str(s) #s#define mollocdefinetostr(mal) _str(mal)static const char devtype[] = mollocdefinetostr(dev_name);static uint32_t devid = dev_id;static const char devdname[] = mollocdefinetostr(dev_name) _ mollocdefinetostr(dev_id) .local;void device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);}
这样,这些配置参数就对其他 include 了这个头文件的文件是可见的了。
至于那句:
#ifndef dev_select #define dev_select dev_abs#endif
这句可有个大好处,所有你想要拥有默认参数,且想要在不同工程中都可以定制的地方都可以这么写。这样,在编译器选项中定义宏,就可以用同一套源码为不同项目生成项目定制代码。
比如在vs中可以在解决方案资源管理器中的项目条目上右键->属性,打开项目的属性页,在 c/c++ ->预处理器->预处理器定义 中定义宏。
codewarrior中则是在edit->standard settings里:
当然,有一点点问题就是这样搞没法使用像前面类枚举那种方法来给宏赋值宏,得直接赋值数字、字符串等。
接下来。what!?还要加设备,这样下去不行!一堆 #if #else 会搞死人的。要是我几十w个设备,难道一个.h文件就几十万行么?我得把配置信息独立出来!
建立一个随便什么名字,甚至随便什么扩展名的文件,扔进工程文件夹,就随便起个名字叫devinfo.txt得了。
devinfo.txt:
// 设备配置信息模板,根据具体设备配置// 设备名,字符串#define dev_name default// 设备id,u32#define dev_id 0
然后修改device模块:
device.h:
#ifndef _device_h#define _device_h#ifndef devinfo_filename #define devinfo_filename devinfo.txt#endifvoid device_printfmsg(void);#endif
device.c:
#include device.h#include #include #define _str(s) #s#define mollocdefinetostr(mal) _str(mal)#include mollocdefinetostr(devinfo_filename)static const char devtype[] = mollocdefinetostr(dev_name);static uint32_t devid = dev_id;static const char devdname[] = mollocdefinetostr(dev_name) _ mollocdefinetostr(dev_id) .local;void device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);}
完美,设备相关信息全部都从外面的txt文件中读出来了,而且这个文件的文件名还是由刚刚才提到的可工程定制的宏配置的方式给出的。我们可以把其他几个设备的配置信息文件都补上。
// 设备名,字符串#define dev_name abs// 设备id,u32#define dev_id 34// 设备名,字符串#define dev_name cba// 设备id,u32#define dev_id 33435// 设备名,字符串#define dev_name lol// 设备id,u32#define dev_id 1234
好了,这样我们只要为所有设备各建立一个txt的信息表,然后当需要切换不同的设备时就用前述方法改一下宏配置切换不同的文件名就好了。
要明白这个方法为什么能起作用,关键是要理解这一句:
#include mollocdefinetostr(devinfo_filename)
我们知道,经过预处理器后,这一句就会变为:
#include devinfo.txt
也许你会想:这是什么鬼,还可以include txt文件?我之前见得怎么都是include .h文件呀。这是一个大大的误区。其实include从来没规定说一定要.h文件,其实可以是任何名字的,这个预处理器指令干的事情就是把include的文件不断递归的文本展开而已。
所以其实上面这句在经过预处理器后会被直接文本替换为对应的文件的内容,一字不差的那种。可能前后会加点注释信息。
所以这种成组绑定、十分固定的配置信息就很适合用这种方式解耦到不同的配置文件中去,按需导入即可。更进一步的,应该要专门为这些配置文件建一个文件夹进行管理。
而对于那种经常会独立更改的配置呢?
一两个的话可以通过之前说的预处理器宏定义的方式来搞定,但是一个稍微有点规模的项目总会涉及到好多好多的配置参数,这个时候就不适合都写在编译器选项里了。
这个时候我会专门建一个工程配置文件,比如就叫app_cfg.h,然后把整个工程中可能用到的宏配置都汇总在这里方便修改,这时之前那种可工程定制的宏写法就特别管用了:
app_cfg.h:
#define devinfo_filename devinfo_cba.txt// 其他宏配置选项 ...
然后,就需要用到强制包含文件这个技巧了,相当于在所有的.c文件前面都直接加一行。
#include app_cfg.h
这是vs2012中的:
这是codewarrior中的:
然后就可以很愉快的在一个文件中操控整个工程了!
那我现在又来需求了,id是有限制的,不能超过5000。那我就这么改。在
#include mollocdefinetostr(devinfo_filename)
下面加一句:
#if(dev_id > 5000) #error device id shouldn't bigger than 5000#endif
那这样,当我们选取cba时就没法通过编译了:
还可以通过。
#ifndef dev_id #error dev_id lost#endif
检查dev_id是否正确进行了宏定义,或如果想要组合的条件:
#if !defined(dev_name) || !defined(dev_id) #error dev_name or dev_id malloc define lost#endif
然后比如某个设备需要进行代码定制处理,一种方法是在代码中直接写语句进行判断当前设备的名字之类的然后执行对应特定语句。
但为了节约编码出来的代码量,同时也是为了体现宏的威力,我们同样可以用预处理指令,遗憾的是,我们没法在预处理器指令中判断字符串,但是可以判断数字,正好我们有id可以用,所以比如我们要让设备abs多输出一行hahaha,那代码就被改成了这样:
void device_printfmsg(void){ printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);#if(dev_id == 34) printf(hahaha);#endif}
记住,这些预处理指令的本质都是在替换文本,所以,只有abs设备时才有这一行代码,对其他设备来说压根没有见到这行代码。
当然,你可以尝试用之前那个include的方法以及其他宏方法来进一步组合定制代码,这是一项创造性工作。
最后突然又想起来一个妙招。也是我最近代码里一直在用的。
我专门搞了一个debugmsg.h,大概长这样:
#ifndef _debug_msg_h#define _debug_msg_h#include #ifdef _debug #define _dbg_printf0(format) ((void)printf(format)) #define _dbg_printf1(format,p1) ((void)printf(format,p1)) ……#else #define _dbg_printf0(format) #define _dbg_printf1(format,p1) ……#endif#endif
这样,所有各个模块中只要引用了这个文件就可以用统一的接口输出调试信息,只要我在主配置文件中定义_debug,所有调试printf就会变成真实的printf,否则就是空语句,无调试信息:
#include debugmsg.hvoid device_printfmsg(void){ _dbg_printf0(device_printfmsg called.); printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);}
那我想要使用这个接口,却又想要为我的device模块单独设一个开关怎么办呢?
整个逻辑简单来说就是,_debug是主开关,其关了所有模块的调试信息都关了,然后各个模块再有各自的开关,必须和_debug一起都被定义才会使这个模块有调试信息。
那我这个模块就改成了这样。
device.h:
#ifndef _device_h#define _device_h// malloc define _device_debug to enable debug message// #define _device_debug#ifndef devinfo_filename#define devinfo_filename devinfo.txt#endifvoid device_printfmsg(void);#endif
device.c:
……#ifndef _device_debug#undef _debug#endif#include debugmsg.hvoid device_printfmsg(void){ _dbg_printf0(device_printfmsg called.); printf(device: %s , devtype); printf(devid: %u , devid); printf(domainname: %s , devdname);}
这样,我只有同时宏定义_ device_debug和_ debug时_dbg_printf0才会被宏定义为printf,否则会被宏定义为空语句,也就没有调试信息了。
这是怎么回事呢?当预处理器读到#ifndef _ device_debug这句发现未宏定义_ device_debug时,它会在下一句取消_ debug的宏定义,这样不管我实际有没宏定义_ debug,当到了#include debugmsg.h并展开后,预处理器都会认为未定义_ debug,所以就会把_dbg_printf0宏定义为空语句,然后就实现了这个串联的逻辑。
后记
好啦,已经讲够多的了,相信你看得也很过瘾。想要再深一步,可以专门看看c语言宏的一些高阶用法。
下次看见哪个库里头到处乱飞的宏配置,不会那么一脸懵逼了吧(# ^ . ^ #)
贸泽电子赞助2021年安森美电源在线研讨会
Avago微型化电压/电流临界值侦测光耦合器问世
世界最大的FPGA芯片搭载433亿个晶体管 拥有1020万个逻辑元件
公有链、私有链、联盟链三者的主要区别是什么?
承德科胜380F标示打码机|铝箔打码机|河北打码机
嵌入式C语言宏配置的相关技巧
AI的机遇只给高学历的人吗
纵维立方持续深化高校合作,基金计划提供创新支援
华为将在华为P30系列发布会上同时发布畅享9S畅享9e和平板M5青春版
PCB技术运用于交通 未来交通将会更加智能
台积电将披露第四季度财报与2023年报,并恢复线下法说会
化学添加剂对KOH 溶液中Si表面反应性的影响
笔记本电脑究竟哪一款才最值得购买呢?
电动车控制器故障自排查方法
人工智能到底是什么跟互联网有着什么关联
铝基板切割分板加工 选择自动换刀分板机主轴SycoTec 4033 DC-T
模拟电路教程 运算放大器比较器电路特性分析
安全性是重要的前提,新能源汽车需进行电池升级
这不科学!AMD 16核旗舰跑分战Intel 10核
灵动微电子与华虹宏力、通富微电共同助力国产智能硬件控制芯片产业链发展