为嵌入式系统编写常量参数的最佳做法 [英] Best practice on writing constant parameters for embedded systems

查看:234
本文介绍了为嵌入式系统编写常量参数的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是static constvs#define在C对于嵌入式系统,

This is a case of "static const" vs "#define" in C" for embedded systems.

在具有传递代码和模块的大型/中型项目中,为您的包含编写常量参数的最佳做法是什么?文件,模块等?

On large/mid projects with "passed-down" code and modules, what is the best practice on writing constant parameters for your include files, modules, etc?

在代码传递中,您不知道您选择的名称是否在其他一些文件中定义,可能使用extern或者在可能包含您的文件的其他文件中的宏来调用。

In a code "passed-down" where you don't know if the names you're choosing are defined in some other included file or might be called with extern or as macros in some other file that might include your file.

拥有以下3个选项:


  1. static const int char_height = 12;

  2. #define CHAR_HEIGHT 12

  3. 枚举{char_height = 12};

  1. static const int char_height = 12;
  2. #define CHAR_HEIGHT 12
  3. enum { char_height = 12 };

哪一个会更好在内存限制不明确的嵌入式系统中)?

which one would be better (on an embedded system with unknown memory constraints)?

原始代码主要用于 #define 但是这些常量在几个方面(即使在相同的文件中也是不同的位置)是随意的实现的,因为似乎有几个人为某个设备开发了这个演示软件。

The original code uses mainly #define's for this, but these kind of constants are haphazardly implemented in several ways (and at different locations even in the same files) since it seems several people developed this demo software for a certain device.

具体来说,这是一个演示代码,显示某个设备的每个硬件和SDK功能。

Specifically, this is a demo code, showing off every hardware and SDK feature of a certain device.


我的大部分数据想到的是用于配置环境的类型:屏幕尺寸,字符集特征,提高代码的可读性。不是在编译器和预处理器可以做的自动配置上。但是由于在那里有很多代码,我害怕全局名称冲突,我不愿使用#define的

Most of the data I'm thinking about is the kind used to configure the environment: screen dimensions, charset characteristics, something to improve the readability of the code. Not on the automatic configuration a compiler and pre-processor could do. But since there's a lot of code in there and I'm afraid of global name conflicts, I'm reluctant to use #define's

目前,我正在考虑从头开始重写项目,并重新实现大部分已经编写的函数,从一个c文件中获取常量,或者将常量的实现重新组织成一个样式。

Currently, I'm considering that it would be better to rewrite the project from scratch and re-implement most of the already written functions to get their constants from just one c file or reorganize the constants' implementation to just one style.

但是:


  1. 这是一个人的项目(所以需要很多时间重新实现一切)

  2. 已实施的代码可以工作,并且已经被修改了好几次。 (如果没有破坏...)


推荐答案

始终考虑可读性和内存限制。另外,宏只是在编译之前发生的复制/粘贴操作。就这样说,我喜欢做以下操作:

Always consider readability and memory constraints. Also, macros are simply copy/paste operations that occur before compilation. With that being said I like to do the following:


  • 我将所有变量定义为 static const 如果要在一个c文件中使用(例如,跨多个文件不可全局访问)。在档案范围内,任何定义为 const 的内容应放在ROM中。显然,这些变量在初始化后不能更改。

  • 我使用 #define 定义所有常量值。

  • 我使用枚举 erations,它增加了可读性。任何你有一个固定值范围的地方,我喜欢枚举来明确表达意图。

  • I define all variables that are constant as being static const if they are to be used in one c file (e.g. not globally accessible across multiple files). Anything defined as const shall be placed in ROM when at file scope. Obviously you cannot change these variables after they're initialized.
  • I define all constant values using #define.
  • I use enumerations where it adds to readability. Any place where you have a fixed range of values I prefer enumerations to explicitly state the intent.

尝试用一个对象来接近项目(即使c不是OO)。隐藏私有函数(不要在标题中创建一个原型),如果可以避免使用全局变量,则不要使用全局变量,将只应驻留在一个c模块(文件)中的变量标记为 static 等。

Try to approach the project with an object oriented perspective (even though c isn't OO). Hide private functions (don't create a prototype in the header), do not use globals if you can avoid it, mark variables that should only reside in one c module (file) as static, etc.

这篇关于为嵌入式系统编写常量参数的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆