Let's take a look at my mistakes:
At that time, I wrote an enumeration similar to the following:
# Ifndef TEST_ENUM_H _
# Define TEST_ENUM_H _
Enum {
TEST_FLAG1_E,
TEST_FLAG2_E,
TEST_FLAG_NR
} TEST_E;
# Endif
"Typedef" was missing before the enum keyword ". I generally use typedef, so that TEST_E instead of enum TEST_E can be used directly.
This header file will be referenced by other source files. Because there is no need to define enumeration variables in the Code, only the enumerated values are used, so no missing "typedef" was found at that time ". Compilation is fine.
However, the build failed on that day. Duplicate TEST_E is defined in the error message, so compilation fails. Due to the time difference with the United States, this error was changed by a colleague in the United States. The reason for his statement's error is that such an enumeration statement is correct for C. -- Our core code is written in C.
However, C ++ defines a TEST_E variable instead of a declaration. As a result, the variable is repeatedly defined. -- Some functional web code is written in C ++.
When I saw his instructions in the morning, I first apologized for break build and despised the makefile of the product-I just joined the product group. In this makefile, in order to check checkin, I have to make clean first to ensure that all the code is compiled. This takes too much time. Third, I remember that this part of web functions use C ++. Fourth, I caught myself down and wrote typedef. Fifth, I was wondering why C ++ compilation went wrong.
But when I saw his statement, I knew he was definitely wrong. For C and C ++, the difference between enum enumeration is not that big. For the above Enumeration type definition, because typedef is missing, TEST_E here treats TEST_E as an enumeration variable, that is, a global variable, whether it is C or C ++. The cause of the problem is that C and C ++ have different processing methods for global variables without initial values-the real interface, and the final compilation link behavior is different.
Let's take a look at the following simple example:
File test1.c
Int;
Int main ()
{
Return 0;
}
File tes2.c
Int;
Compile
[Fgao @ fgao-vm-fc13 test] $ gcc-g-Wall test1.c test2.c
[Fgao @ fgao-vm-fc13 test] $
Compilation does not contain any warnings or errors. It is a weak symbol for global variables without an initial value. For multiple weak symbol definitions, there will be no problems in the C link stage. See my article to study the differences between the BSS and COMMON segments by failing to initialize global variables: http://www.bkjia.com/kf/201202/118030.html
In this article, I explained why C allows multiple weak symbols to exist.
It is considered as C ++ code and compiled using g ++:
[Fgao @ fgao-vm-fc13 test] $ g ++-g-Wall test1.c test2.c
/Tmp/ccQdTwRi. o :(. Bss + 0x0): multiple definition of 'A'
/Tmp/cc7SOWD1. o:/home/fgao/works/test/test1.c: 4: first defined here
Collect2: ld returned 1 exit status
It is also a global variable without an initial value, and an error will be reported in the Link phase of C ++. For C ++ errors, this must be due to the Link Mechanism of C ++. I don't know the reason. If you know me, please kindly advise. Thank you.
From the blog