UC/OS-II learning notes-defining global variables

Source: Internet
Author: User

Global variables should be variables that are allocated in memory and can be called by other modules using the extern keyword in C language. Therefore, it must be defined in the. C and. H files. This repetitive definition can easily lead to errors. The following methods can be defined only once in the header file. Although it is a bit difficult to understand, once the user Master, it is flexible to use. The definitions in Table 1.2 appear in the. h header file that defines all global variables.

Program list L 1.2 defines a Global Macro.
# 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: # ifdef OS _globals
# Define OS _ext
# Else
# Define OS _ext extern
# Endif
 
OS _ext int32u osidlectr;
OS _ext int32u osidlectrrun;
OS _ext int32u osidlectrmax; at the same time, ucos_ii.h is defined as follows: # 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 osidlectrrun;
Int32u osidlectrmax;
 
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 osidlectrrun;
Extern int32u osidlectrmax;
 
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. Summary: define a global variable in the embedded C language and expect this variable to be used in other C files and in engineering assembly. We define a global variable in the header file and use the extern keyword to reference it in other files. If each file references this header file, the problem of repeatedly defining global variables will occur. We can solve this problem by using this Conditional compilation.

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.