views:

245

answers:

4

vector is included in only one source file. The only stl include in the header files is string. Yet I cannot get rid of multiple definition errors (example below). Any ideas?

./plugin_dfb.o:mipsel-linux-uclibc/include/c++/4.2.0/bits/stl_bvector.h:182: multiple definition of `std::operator-(std::_Bit_iterator_base const&, std::_Bit_iterator_base const&)' ./painter.o:mipsel-linux-uclibc/include/c++/4.2.0/bits/stl_bvector.h:182: first defined here

A: 

have you tried using #pragma once?

Himanshu
#pragma once would *not* solve a linking error
sellibitze
A: 

I think you have included the vector in only one header file but that header file is not having the #define block, this is resulting the redefinition of the vector header file included in it. Please enclose your include file in the template given below and try.

 #if !defined(HEADER_FILE) 
 #define HEADER_FILE
 // Your code
 #endif
GJ
it usually goes more like `#ifndef HEADER_FILE_H`, `#define HEADER_FILE_H` and `#endif` in C / C++
Adam Luchjenbroers
A: 

As the problem appears during linking it looks to be related to template instantiation. Given instantiation implementation specifics the template functions/definitions should be put in the common include files to assure they are visible everywhere and do not duplicate header includes what may be the case here.

From what you posted problem concerns the operator - that may be used by std::distance() that may be called from find() type functions. So look if you have such functions defined or called as they may work with vectors and strings and make sure they are in shared header files.

jszpilewski
+1  A: 

This std::operator- is an inline function with external linkage. It appears the linker doesn't support multiple definitions of such inline functions. But the "one definition rule" of C++ (ODR) clearly allows this. Typically such symbols get "weak linkage" (GNU terminology) but I think both, the object file format and the linker, need to support this.

I would try to ask a question in a group / mailing list dedicated to your platform. Something along the lines of "Does platform X support C++ with respect to linking and the C++-related one-definition rule for inline functions and templates?".

You may also want to check the GCC documentation. It's possible that they provide a command line switch as a work-around. If you don't already use g++ as front-end to the linker, you should try it. Maybe that helps, too.

sellibitze