多重定义错误(collect2: error: ld returned 1 exit status)

Posted

技术标签:

【中文标题】多重定义错误(collect2: error: ld returned 1 exit status)【英文标题】:Multiple definition error (collect2: error: ld returned 1 exit status) 【发布时间】:2020-09-07 11:57:27 【问题描述】:

我使用https://github.com/jdduke/three_cpp 作为仅标头模式,但在我的项目编译时遇到了一些问题。

当我在多个 C++ 文件中包含以下 matrix4.hpp header(仅包含必要部分)时会出现此问题

three/core/matrix4.hpp(删除不需要的部分)

#ifndef THREE_MATRIX4_HPP
#define THREE_MATRIX4_HPP

#include <three/common.hpp>

#include <three/core/math.hpp>
#include <three/core/quaternion.hpp>
#include <three/core/vector3.hpp>
#include <three/core/vector4.hpp>

namespace three 

class Matrix4 
public:
  // Function without any erros
  THREE_DECL Matrix4();
  THREE_DECL Vector3    getScale() const;

  // Error causing functions
  Vector3 getColumnX() const;
  Vector3 getColumnY() const;
  Vector3 getColumnZ() const;
  Matrix4& setPosition( const Vector3& v );
;

 // namespace three

#if defined(THREE_HEADER_ONLY)
# include <three/core/impl/matrix4.ipp> // This is where all this functions is defined.
#endif // defined(THREE_HEADER_ONLY)

#endif // THREE_MATRIX4_HPP

three/core/impl/matrix4.ipp(删除不需要的部分)

#ifndef THREE_MATRIX4_IPP
#define THREE_MATRIX4_IPP

#include <three/core/matrix4.hpp>

namespace three 

Matrix4::Matrix4() 
  identity();


Matrix4::Matrix4( const Matrix4& other ) 
  copy( other );


Vector3 Matrix4::getScale() const 
  auto sx = Vector3( te[0], te[1], te[2] ).length();
  auto sy = Vector3( te[4], te[5], te[6] ).length();
  auto sz = Vector3( te[8], te[9], te[10] ).length();
  return Vector3( sx, sy, sz );


Vector3 Matrix4::getColumnX() const 
  return Vector3( te[0], te[1], te[2] );


Vector3 Matrix4::getColumnY() const 
  return Vector3( te[4], te[5], te[6] );


Vector3 Matrix4::getColumnZ() const 
  return Vector3( te[8], te[9], te[10] );


Vector3 Matrix4::getPosition() const 
  return Vector3( te[12], te[13], te[14] );


 // namespace three

#endif // THREE_MATRIX4_IPP

当从多个C++文件中包含三个/core/matrix4.hpp时,产生多个定义错误

/usr/bin/ld: /tmp/ccmYWwMM.o: in function `std::_Rb_tree_const_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, bool> >::_Rb_tree_const_iterator(std::_Rb_tree_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, bool> > const&)':
/home/three/core/impl/matrix4.ipp:617: multiple definition of `three::Matrix4::setPosition(three::Vector3 const&)'; /tmp/ccSaHLVI.o:/home/three/three/core/impl/matrix4.ipp:617: first defined here
/usr/bin/ld: /tmp/ccmYWwMM.o: in function `three::Vector3::subSelf(three::Vector3 const&)':
/home/three/three/core/impl/matrix4.ipp:627: multiple definition of `three::Matrix4::getColumnX() const'; /tmp/ccSaHLVI.o:/home/three/three/core/impl/matrix4.ipp:627: first defined here
/usr/bin/ld: /tmp/ccmYWwMM.o: in function `three::Matrix4::getColumnY() const':
/home/three/three/core/impl/matrix4.ipp:631: multiple definition of `three::Matrix4::getColumnY() const'; /tmp/ccSaHLVI.o:/home/three/three/core/impl/matrix4.ipp:631: first defined here
/usr/bin/ld: /tmp/ccmYWwMM.o: in function `three::Matrix4::getColumnZ() const':
/home/three/three/core/impl/matrix4.ipp:635: multiple definition of `three::Matrix4::getColumnZ() const'; /tmp/ccSaHLVI.o:/home/three/three/core/impl/matrix4.ipp:635: first defined here
collect2: error: ld returned 1 exit status

因此,根据我们的试验,我们注意到 THREE_DECL 在 matrix4.hpp 中的以下函数中没有提及

Vector3 getColumnX() const; Vector3 getColumnY() const; Vector3 getColumnZ() const; Matrix4&amp; setPosition( const Vector3&amp; v );

所以在修改这些函数后,如下错误是固定的

THREE_DECL Vector3 getColumnX() const; THREE_DECL Vector3 getColumnY() const; THREE_DECL Vector3 getColumnZ() const; THREE_DECL Matrix4&amp; setPosition( const Vector3&amp; v );

基于three/config.hpp,如果使用仅标头版本,THREE_DECL 定义为inline

我不明白的是

    为什么同一个头文件包含在不同的编译单元中,也会出现多重定义错误? 添加THREE_DECL 对解决这些问题有何意义?

任何帮助将不胜感激。

【问题讨论】:

是时候了解translation units了。因为您在头文件中包含函数定义(实现),所以包含"matrix4.hpp" 头文件的每个翻译单元都将具有这些函数定义。而the One Definition Rule (ODR) 不允许这样做。 【参考方案1】:

这是由于基于 ODR 的限制。

为什么同一个头文件包含在不同的编译单元中,也会出现多重定义错误?

正如 cmets 中所述,这是由于 One Definition Rule(ODR) 中的限制。

单一定义规则 (ODR) 是 C++ 的重要规则 规定对象和非内联的编程语言 函数在整个程序中不能有多个定义 并且模板和类型不能有多个定义 翻译单元。它在 ISO C++ 标准(ISO/IEC 14882) 2003,第 3.2 节。

所以它也回答了你的第二个问题。

添加THREE_DECL 对解决这些问题有何意义?

如果您检查pros of using inline functions,它会为您提供inline 关键字的使用意义。

通过将其标记为内联,您可以将函数定义放在标题中 文件(即它可以包含在多个编译单元中,没有 链接器抱怨)。

因此,如果您将函数更改为inline,则不会出现多个定义。

【讨论】:

以上是关于多重定义错误(collect2: error: ld returned 1 exit status)的主要内容,如果未能解决你的问题,请参考以下文章

QT C++ 新手问题 error: collect2: ld returned 1 exit status。请高手指教!

QT 错误 :-1: error: collect2: ld 返回 1 这是啥错误 求大神指导

Qt编译中出现collect2.exe:-1: 错误:error: ld returned 1 exit status

collect2:错误:ld 以信号 11 [分段错误] 终止,核心转储

collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped

collect2:错误:ld 返回 1 退出状态,gcc