1681 views|0 replies

6555

Posts

0

Resources
The OP
 

The problem of repeated declaration of global variables in embedded C programming [Copy link]

When writing programs in C language, we often encounter such a situation: we want to define a global variable in the header file, and then include it in two different c files, hoping that this global variable can be shared by the two files. For example, a global variable int i is declared in the header file led.h of the subfunction led.c, and led.h is included in main.c and led.c, so some people naively think that it can be used normally, but when you compile, the following error will appear: ..\OBJ\test.axf: Error: L6200E: Symbol TimingDelay multiply defined (by systick.o and main.o). That is to say, the compiler thinks that we have defined the variable i repeatedly. This is because the #include command moves the contents of the header file to the #include location intact, so it is equivalent to executing int i once in main.c and led.c. Global variables in C language are visible within the project (or engineering), which results in two variables i in the same project, and the compiler considers it a duplicate definition. The correct solution is as follows: [size=4 ](1)main.c #include"led.h" [color=# 000000] int i; //Define a global variable (2)led.c [size=4 ] #include"led.h" [color=#000000 ]extern int i;//Declare this variable as an external variable, which is a global variable defined in other c files. Note that declaration is different from definition. Definition will allocate variable space, while declaration will not allocate space. It just tells the compiler that this variable is somewhere else and I am just using it.

This post is from DSP and ARM Processors
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list