矢量多重定义链接错误

发布于 2024-08-16 03:32:12 字数 335 浏览 12 评论 0原文

矢量仅包含在一个源文件中。头文件中唯一包含的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

今天小雨转甜 2024-08-23 03:32:12

此 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.

懒的傷心 2024-08-23 03:32:12

您尝试过使用#pragma Once吗?

have you tried using #pragma once?

爺獨霸怡葒院 2024-08-23 03:32:12

我认为您仅将向量包含在一个头文件中,但该头文件没有 #define 块,这导致重新定义其中包含的向量头文件。
请将您的包含文件包含在下面给出的模板中并尝试。

 #if !defined(HEADER_FILE) 
 #define HEADER_FILE
 // Your code
 #endif

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
无风消散 2024-08-23 03:32:12

由于问题是在链接过程中出现的,因此它看起来与模板实例化有关。给定实例化实现细节,模板函数/定义应该放在公共包含文件中,以确保它们在任何地方都可见,并且不会重复标头包含,这里可能出现这种情况。

从您发布的内容来看,问题涉及运算符 - 可能由 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文