In the past few days, I have studied the source code of uC/OS II and found that the program design method provided by it can effectively solve the problem of the above title. It is mainly implemented through the # ifdef preprocessing command.
Let's talk about the confusion caused by extern!
Suppose there are five files: Main. C, hello. C, flow. C, etc. C, include. h.
If some global variables are defined in Main. C, as follows:
Char obu_channel
Char obu_info
Char obu_mac
However, we need to use these variables in hello. c flow. c etc. C. We usually add the extern keyword to these three files, and then we can use them normally. However, this does not make the program concise. If it is a small program, it doesn't matter if it is written. But if it is a large project or project, we should consider this problem: if every *. c files call global variables of other files. If there are many global variables of this type, how do you manage them effectively to make it concise? Or at a certain point in time, if you want to view the specific definition location of this variable, you may not be able to start. You see, it's confusing! Okay, let's solve it.
We can define these variables in the include. h file. The definition is as follows:
# Ifdef global
# DEFINE _ ext _
# Else
# DEFINE _ ext _ extern
# Endif
_ Ext _ char obu_channel
_ Ext _ char obu_info
_ Ext _ char obu_mac
Then, in Main. C contains include. h header file, and in main. c: # define global is added, and the compiler allocates the memory for it during compilation, which is equivalent to the main. c defines three global variables. As defined in Main. C:
Char obu_channel
Char obu_info
Char obu_mac
If hello. C and flow. C and etc. c when these three files need to use these three variables (if we do not define # define global) _ ext _ will be replaced with extern, and the system will automatically add them as extern, when using a table, these variables are defined by other files. The following is the case:
Extern
Char obu_channel
Extern
Char obu_info
Extern
Char obu_mac
In this way, you can easily manage global variables in a unified manner, and make the program more concise. This method is fine !~~ This is also a small trick. It seems that ucos ii still has some ideas. You can use the programming methods in it to improve your skills. It is always good to study the masterpiece of masters. Come on !~~