矢量多重定义链接错误
矢量仅包含在一个源文件中。头文件中唯一包含的 stl 是 string。然而我无法摆脱多个定义错误(下面的示例)。有什么想法吗?
./plugin_dfb.o:mipsel-linux-uclibc/include/c++/4.2.0/bits/stl_bvector.h:182: `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:首先在这里定义
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此 std::operator- 是具有外部链接的内联函数。链接器似乎不支持此类内联函数的多个定义。但C++(ODR)的“单一定义规则”明确允许这一点。通常,此类符号会获得“弱链接”(GNU 术语),但我认为目标文件格式和链接器都需要支持这一点。
我会尝试在专门针对您的平台的群组/邮件列表中提出问题。类似于“平台 X 在链接方面支持 C++ 以及内联函数和模板的与 C++ 相关的单一定义规则吗?”。
您可能还想查看 GCC 文档。他们可能提供命令行开关作为解决方法。如果您尚未使用 g++ 作为链接器的前端,您应该尝试一下。也许这也有帮助。
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.
您尝试过使用
#pragma Once
吗?have you tried using
#pragma once
?我认为您仅将向量包含在一个头文件中,但该头文件没有 #define 块,这导致重新定义其中包含的向量头文件。
请将您的包含文件包含在下面给出的模板中并尝试。
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.
由于问题是在链接过程中出现的,因此它看起来与模板实例化有关。给定实例化实现细节,模板函数/定义应该放在公共包含文件中,以确保它们在任何地方都可见,并且不会重复标头包含,这里可能出现这种情况。
从您发布的内容来看,问题涉及运算符 - 可能由 std::distance() 使用,而 std::distance() 可能从 find() 类型函数调用。因此,请检查您是否定义或调用了此类函数,因为它们可以与向量和字符串一起使用,并确保它们位于共享头文件中。
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.