Global variable declaration in UCOS-II

xiaoxiao2021-03-06  23

/*statement:

Author: Ji Guorui. All rights reserved http://blog.9cbs.net/ji_gr

Please do not share without permission

In the code of UC / OS-II, a new global variable definition method is applied. In "UC / OS-II", the author gives an explanation, but here still can't help but discuss it.

We know that there are two problems in the use of global variables:

1. To assign a memory to it, and can only be assigned once. 2. You must also be called via the extern keyword in other modules.

According to the usual definition, if the arrangement is not good, it will always have problems, and extern is not finished.

And in UC / OS-II has a relatively clever approach.

In UC / OS-II, each .c file contains all .h files through incruDes.h

Then, UC / OS-II uses the following approach:

Whenever the global variable is defined, first do the following definition:

#ifdef xxx_globals # define xxx_ext # else # define xxx_ext extern # Endif

Each global variable in the .h file adds the prefix of xxx_ext. XXX represents the name of the module. And there is the following definition in the .c file of the module:

#define xxx_globals # include "incrudes.h"

For example, XXX_ext int32u g_xxx is defined; when compiling to the XXX module, XXX_Global is defined, according to the above precompiled conditions, XXX_ext is empty, so XXX_EXT INT32U g_xxx; equivalent to INT32U g_xxx;

At this time, the variable is allocated.

When compiling other modules, XXX_Global is not defined, according to the above precompiled conditions, XXX_ext is extern, thus

XXX_EXT INT32U g_xxx; equivalent to Extern INT32U g_xxx;

At this time, just the external reference of the variable.

Through this approach, the global variable is only necessary to define once in the header file, avoiding a lot of trouble.

转载请注明原文地址:https://www.9cbs.com/read-51095.html

New Post(0)