Fixed repeated variable definitions when multiple files are created in Keil
Source: Internet
Author: User
Fixed repeated variable definitions when multiple files are created in Keil Extern is used in many places in the C/C ++ language. However, if you do not really understand its meaning, it will bring a lot of trouble to programming, let's explain it in detail.
For small programs, there is usually only one C file and one head file. We usually define global variables directly in the C file and add int I Definitions before the program. If you want to define the following two methods in the header file: Use extern to declare: extern int I; this statement only declares the variable I, int I must be added before the program in the C file. Extern int I = 0; both the Declaration and definition are done.
For larger programs, there are many c files and header files. At this time, the global variables must be declared in the header file and then defined in a C file. If it is defined in the header file, a duplicate definition error occurs during compilation. If only the header file is declared, there will be a warning that the definition is not defined.
* ** Error l104: multiple public Definitions
Symbol: K
Module: 222. OBJ (222)
The above error occurs because the variable k is repeatedly defined. Add extern before the variable definition in your header file, and then define it before a program in a C file.
* ** Warning L1: unresolved external symbol
Symbol: K
Module: 111. OBJ (111)
* ** Warning L2: reference made to unresolved external
Symbol: K
Module: 111. OBJ (111)
Address: 000dh
* ** Warning L1: unresolved external symbol
Symbol: K
Module: 222. OBJ (222)
* ** Warning L2: reference made to unresolved external
Symbol: K
Module: 222. OBJ (222)
Address: 001fh
The four warnings indicate that the variable k is declared in the header file but not defined. You only need to define it before a program in a C file.Article reposted links: http://blog.sina.com.cn/s/blog_4d8890ad01000ajw.html
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.