STM32 Learning 009_ Definition and declaration of global variables

Publisher:Tiger8Latest update time:2017-10-26 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Today, when I was writing the interrupt function SysTcik_Handler(), I always got an error. The global variable extern u16 ntime was defined at the beginning (ntime is written in the systick interrupt function of stm32f10x_it.c), but it always got an error when compiling. I found a solution after searching on Baidu.STM32 Learning 009_ Definition and declaration of global variables

I have noticed extern variables before, but I have not thought carefully about how to use them. Today, I came across them and I must understand them and the general role of this keyword (just the general role, I dare not talk about the profoundness of C);

When you build a header file library, you often encounter a problem, that is, define a template variable in file A, and want to pass its value to file B for use, but the function in file A cannot have a return parameter, such as an interrupt service function, what should you do? If you define the template in A, and then include Ah and Bh in includes.h, and then put includes.h in Ac and Bc, there will be no problem in compiling them separately, but there will be problems when linking.

“Symbol temple multiply defined(by A.o and B.o)”

This means that the variable has been defined multiple times!!!

The solution is:

After defining the template variable in A, just declare it in B using extern, for example:

1. Define temple in A and assign value: u16 temp2=0;

STM32 Learning 009_ Definition and declaration of global variables

2.Declare extern u16 temp2 in B;

STM32 Learning 009_ Definition and declaration of global variables

This is just a declaration, no more assignment, otherwise an error will be reported!

Symbol temp2 multiply defined (by catch_pwm.o and app.o)

Here we should pay attention to the difference between variable definition and variable declaration:

Variable definition uses the form of "data type + variable name", and the compiler needs to allocate memory units for it;

The variable declaration uses the form of "extern variable type + variable name", which tells the compiler that this variable will be defined in other external C files, and I will only use it externally. The compiler will not allocate memory space for it, and will allocate memory space for it when it actually encounters the variable definition.

//////////////////The following is a post by “Juhai”////////////////////////////////////

1. Define ordinary variables as global variables.
     If it is an ordinary type, you don't need a *.h file at all. Define it directly in the *.c file and use extern declaration in the calling file, because the compiler can recognize ordinary types. For example, in a my.c file, I define char name[10]; then in other files, just use extern char name[] (because it is a declaration, the size of a one-bit array can be omitted, but it is not recommended to use pointers, comparing pointers and arrays are two different things) external declaration, tell the compiler that I have defined this variable, and how to find out. This is in line with common sense, because char is a type that the compiler can recognize independently.
     2. Define custom structure types as global variables
     are different from ordinary types. If you don't notify the compiler in advance, the compiler will not recognize your custom type. At this time, the *.h file appears. Doesn't defining structure types not occupy memory? Well, I put the definition of my large structure in the *.h file. In this way, no matter how many times you incude, the memory will not be occupied. And there is another advantage of this. You can include this *.h file in other files. In this way, in this file, the compiler can recognize your custom type. Isn't the purpose achieved? If I define
typedef struct _POSITION
{
        int x;
        int y;
}POSITION; in global.h
, then I can define the global variable in a global.c file, but I have to include that *.h file, such as
 
include "global.h"
POSITION current,;  
In this way, the variable cunrrent is defined. When referencing this variable in other files, just declare it with extern POSITION current;, and then you can use it. However, this file also has to include "global.h" because if it is not included, the POSITION type will not be recognized in this file.

 

1. How to reference a global variable that has already been defined?   

Answer: extern can be used by referencing header files or using the extern keyword. If you use the header file reference method to reference a global variable declared in the header file, assuming you have written it incorrectly, an error will be reported during compilation. If you use the extern method to reference it, assuming you have made the same mistake, no error will be reported during compilation, but an error will be reported during linking.

2. Can global variables be defined in multiple .C files? Why?   

A: Yes, you can declare global variables with the same name in different C files in the form of static. You can declare global variables with the same name in different C files, provided that only one of the C files assigns an initial value to the variable, and then the connection will not go wrong.

OVER!


Keywords:STM32 Reference address:STM32 Learning 009_ Definition and declaration of global variables

Previous article:STM32 Learning 008_Analysis of ARM Products
Next article:STM32 Learning 011_Product Development Process (I)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号