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.
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;
2.Declare extern u16 temp2 in B;
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!
Previous article:STM32 Learning 008_Analysis of ARM Products
Next article:STM32 Learning 011_Product Development Process (I)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Impedance Matching and the Smith Chart: The Basics
- ATH10 temperature and humidity measurement is inaccurate
- Happy Little New Year! How many days off do you have for the New Year?
- Advantages of using pressure sensors in constant pressure water supply systems!
- One-to-two high power relay
- CC2640 Button Battery Powered Design Tips
- A new energy company in Beijing is recruiting algorithm and HIL engineers
- Review summary: Free review of Pingtouge scenario-based Bluetooth Mesh gateway development kit
- MOSFET capacitor
- 5G small base station construction technology article sharing