/Home/TACE/openav/source/seamlessmessage/cpaoflt. O: In function 'cpaoflt: get_m_strprmair () const ':
Cpaoflt. cpp :(. Text + 0x0): Multiple definition of 'cpaoflt: get_m_strprmair () const'
/Home/TACE/openav/source/seamlessmessage/cpaoflt. O: cpaoflt. cpp :(. Text + 0x0): first defined here
During the compilation process, GCC reports the multiple definition of function errors, mainly including the following:
1. variables or functions are defined in the header file, rather than declarations. For example, for foobar. H,
------------- Foobar. h -----------
Int Foo = 10;
Int bar ()
{
Return 10;
}
------------- Foobar. h -----------
When foobar. H is referenced multiple times by the same. c file (which may be indirectly referenced multiple times), compile and generate the corresponding. o file. When linking, the foo bar is repeatedly defined.
One possible method is to add a macro definition to the header file to prevent repeated references to the header file, for example:
------------- Foobar. h -----------
# Ifndef foobar_h __
# Define foobar_h __
Int Foo = 10;
Int bar ()
{
Return 10;
}
# Endif/* foobar_h __*/
------------- Foobar. h -----------
If you only have one. c file, you may get a program that can run. However, if you have two or more. c files that reference foobar. H, such as test1.c
If test2.c references foobar. H, the GCC-o a. Out test1.c test2.c still reports a duplicate definition error during the link.
Solution: declare only functions and variables in the header file. For inline functions in C ++, define them in the header file (the inline function should be used during compilation ).
You can modify foobar. h to create a corresponding. c file for the. h file, declare the function defined in the. c file in the. h file, and add the modifier extern in front of the variable.
------------- Foobar. h -----------
# Ifndef foobar_h __
# Define foobar_h __
Extern int Foo;
Int bar ();
# Endif/* foobar_h __*/
------------- Foobar. h -----------
------------- Foobar. c -----------
Int Foo = 10;
Int bar ()
{
Return 10;
}
------------- Foobar. c -----------
In addition, the static modifier can also restrict the scope of variables and functions, but it has nothing to do with this article.
2. use the command gcc-O foobar main. O foobar. O foobar. o, you may think about how to write such stupid commands, but if the project is large ,. O when there are many files, foobar. O is referenced repeatedly inadvertently. For example, in makefile:
Foo_obj = foo1.o foo2.o foo3.o foobar. o
Bar_obj = bar1.o bar2.o bar3.o foobar. o
Foobar: Main. o $ (foo_obj) $ (bar_obj)
Gcc-O foobar main. o $ (foo_obj) $ (bar_obj)
Errors occur and are difficult to find out, especially when maintaining programs of others. Unfortunately, it took me two days to find out the cause. Although I had doubts about this problem for a long time, I ignored it. You can modify it as follows:
Foobar: Main. o $ (foo_obj) $ (bar_obj)
Gcc-o $ @ $ ^
Aha, I finally know why to set the $ ^ variable for makefile. (Note: $ ^ all dependent files are separated by spaces)
3. During GCC compilation, add the-xlinker-zmuldefs option, but this only forces the compiler to do some work and the program error is still not corrected. -Xlinker tells GCC to pass the zmuldefs option to the linker lD, that is, to force the linker to ignore repeated definitions.
[Transfer] http://blog.csdn.net/ninthing/article/details/6014088
-------------------
I wrote all the global variables in a global. h file and included them in other files, so the multiple definition .....
(Compiler GCC)
Later, I found many similar errors on the Internet, and everyone had their own troubles.
My code structure
Main. cpp
# Include "Global. H"
Winmain (....)
{
...
}
File_1.cpp
# Include "Global. H"
....
File_2.cpp
# Include "Global. H"
...
Since each file in the project is interpreted independently,
(Even if the header file has
# Ifndef _ x_h
....
# Enfif)
If other files contain global. H
Each file is generated to generate an independent identifier. When the compiler is connected, all the symbols in the project are integrated. Because there are duplicate variables in the file, a duplicate definition error occurs.
The solution is as follows:
Declare the variable in global. C (or. cpp) and create a head file global. h.
Add extern before all variable declarations...
For example, extern handle ghevent;
Note that there are no initialization statements for variables.
The. h file is included in other CPP files that require global variables instead of the. cpp file. The compiler is global. cpp.
Generate the target file, and then connect to it in the file that uses global variables.
Certificate --------------------------------------------------------------------------------------------------------------------------------------
During makefile today, the error multiple definition of is always prompted, that is, repeated variable definition.
However, my program clearly does not have multiple definitions, and I don't know what is going on. I use all header files with header file protection characters.
I tried it, but it still didn't work. I finally searched the internet. The solution is actually very simple, that is, the compilation error.
The prompt indicates that the static qualifier is used in the first definition. It is OK if it is only used in this file.
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Note: When encountering such problems, pay attention to the following aspects: 1. do not include the header file repeatedly, which will lead to repeated definitions.
2. WhenMultiple CC files must contain the same header fileStatic member variables are repeatedly defined. One solution is to make only declarations on such variables in the. h file, such as extern.
Int
A, and in another. CC file to use this variable for definition initialization. However, this solution is not applicable when multiple. c files need to contain this header file. An error occurs because the. c file of this variable is not required to compile to extern.
Int
A does not have the corresponding definition or link, but the definition is repeated because it is a static variable. So in this case, it is best. in the H file class, delete the member variable of this class, and only in. file C is declared and defined, for example, in file. c file Definition
Static int A =
0. You may not delete this member variable from the header file, but combine multiple. CC files containing this header file into one file. However, this is not a good choice and is good for reading the code.
[Transfer] http://blog.sina.com.cn/s/blog_620882f40101199u.html