GCC: Multiple definition of [conversion]

Source: Internet
Author: User

/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. But if you have two or more. file C references foobar. h. For example, test1.c test2.c references foobar. h, then gcc-o. out test1.c test2.c, the duplicate definition error will still be reported 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 [email protected] $ ^

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)

As long as global. H is included in other files, it will be interpreted independently, and each file will be 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 then create a head file global. h to add extern before all the 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 will generate the target file for global. cpp, and then connect to the file when using 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. This will lead to repeated definitions. 2. WhenMultiple CC files must contain the same header fileStatic member variables are repeatedly defined. One solution is to declare the variables in the. h file, for example, externinta, and initialize the definition in the. CC file to use the variable. But this solution is applicable to multiple. c files must contain this header file, which is not applicable and may cause errors, because this variable is not required. when the c file is compiled to externinta, the corresponding definition cannot be found or linked, 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. file C defines static int A = 0. or remove the member variable from the header file, and include multiple. CC files are merged into one file, but this is not a good choice. For good readability of the Code

 

[Transfer] http://blog.sina.com.cn/s/blog_620882f40101199u.html

GCC: Multiple definition of [conversion]

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.