In other words, when the code is sorted out, an original definition is accidentally defined in. move the global variables in file C. h file. c file), and then suddenly cannot generate. ko (the driver module that can be dynamically loaded in Linux ). what's going on? Is it because I made a mistake when I moved the code? Take a closer look at the prompt. It turns out to be multiple definition. the previous Code defines the global variable in file ***. c, and then in other. in C, the extern import is used and can be compiled smoothly at that time. ko. later, due to the poor appearance of the code, the code was mentioned as file ***. h. metropolis
Include File ***. H (doesn't it look good? It doesn't matter for the time being). How can I move the code so easily that the problem will arise? (C code has not been written for a while, ^ _ ^ ...).
As a result, I started to write a simple test code to find the cause. The following sections describe the code that people around the world can understand:
// File1.h
# Ifndef _ file1_h __
# DEFINE _ file1_h __
Int I;
# Endif
// File2.h
# Ifndef _ file2_h __
# DEFINE _ file2_h __
# Include "file1.h"
Extern void file2_change_ I _value (void );
# Endif
// File1.c
# Include "file1.h"
# Include "file2.h"
Void main (void)
{
Printf ("before I = % d/N", I );
File2_change_ I _value ();
Printf ("after I = % d/N", I );
}
// File2.c
# Include "file2.h"
Void file2_change_ I _value (void)
{
I = 10;
}
I am using the Linux platform, so I use GCC file1.c file2.c-o file to compile and generate an executable file. Why ?? This time, the file is generated smoothly. Execute./file quickly, and the console prints the following information:
Before I = 0
After I = 10
Let's look back at the GCC compilation process and see if there is any error or warning information similar to multiple definition. Unfortunately, there is no such information. isn't this small code written now simulate the above generation. the original code of the Ko file. the above code is changed to the following. You only need to modify the file1.h file.
// File1.h
# Ifndef _ file1_h __
# DEFINE _ file1_h __
Int I = 0;
# Endif
Then execute GCC file1.c file2.c-o file. Haha, this time the multiple definition of 'I' prompt appears.
Now let's compare the difference between two file1.h operations. Obviously, for the first time, no initial value is assigned to I, and for the second operation, the initial value is 0. Why? It seems that the initial values of int global variables are all 0. Think about it. Since I 've been learning C language for 8th years, I 've forgotten about books. now that the global variable I has the same value twice, why is this prompt? Oh, by the way, it seems that once, it is also in. in file C, a global variable is defined and the initial value is assigned. Then, the definition statement is copied to other variables that use it. in the c file, an extern is added in front, and the compilation is also failed. are the phenomena in these two very similar ??
Look at the C language textbook. It seems that you can assign an initial value when defining it. You don't need to assign a value when declaring it. You just need to execute the extern command and then OK. as for. h defines global variables. In general, multiple variables are used in this case. c file in include. h later, the variable will be redefined as another variable, and the global variable name will be contaminated, and the multiple definition will be prompted. I personally think this is the case. If there is something incomplete or inappropriate, you can leave a message .....