Go deep into the usage of global variables in uCOS

Source: Internet
Author: User

Global variables are often encountered in C programming. The following method is generally used to define global variables:

. C file Definition

Unsigned int gVariable;

. H file Declaration

Extern unsigned int gVariable;

This aims to prevent repeated declarations of variables and improve compilation efficiency. However, if such a plan is not properly arranged, various problems may occur. Repeated declarations during compilation, or even errors.

After reading the book "uC/OS-II" Translated by Shao Beibei, I found that a very clever global variable definition method was used in it. I will record my understanding below, your own notes. Also, write it out to learn with everyone.

The uC/OS-II defines a header file that is globally used. This file is referenced in any. C file.

This macro is defined in each. h file. \

Copy codeThe Code is as follows: # ifdef XXX_GLOBALS

# Define XXX_EXT

# Else

# Define XXX_EXT extern

# Endif

In the. H file, each global variable is prefixed with xxx_EXT. Xxx indicates the name of the module. The. C file of this module has the following definitions:

# Define XXX_GLOBALS

# Include "des. h"

When the compiler processes the. C file, it forces xxx_EXT (which can be found in the corresponding. h file) to be null (because xxx_GLOBALS has been defined ). Therefore, the compiler allocates memory space for each global variable. When the compiler processes other. C files, xxx_GLOBAL is not defined, and xxx_EXT is defined as extern, so that you can call external global variables. To illustrate this concept, see uC/OS _II.H, which includes the following definitions:

Copy codeThe Code is as follows: # ifdef OS _GLOBALS

# Define OS _EXT

# Else

# Define OS _EXT extern

# Endif

OS _EXT INT32U OSIdleCtr;

OS _EXT INT32U OSIdleRun;

OS _EXT INT32U OSIdleMax;

At the same time, uCOS_II.H has the following definitions:

# Define OS _GLOBALS

# Include "des. h"

When the compiler processes uCOS_II.C, it turns the header file into the following, because OS _EXT is set to null.

INT32U OSIdleCtr;

INT32U OSIdleRun;

INT32U OSIdleMax;

In this way, the compiler will allocate these global variables to the memory. When the compiler processes other. C files, the header file becomes as follows. Because OS _GLOBAL is not defined, OS _EXT is defined as extern.

Extern INT32U OSIdleCtr;

Extern INT32U OSIdleRun;

Extern INT32U OSIdleMax;

In this case, no memory allocation is generated, and these variables can be used in any. C file. In this way, you only need to define it once in the. H file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.