我使用https://github.com/jdduke/three_cpp作为仅标头模式,但在使用我的项目编译时遇到了一些问题。
当我在多个 C++ 文件中包含以下matrix4.hpp 标头(仅包含必要部分)时,会出现此问题
三/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
三/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& setPosition( const Vector3& v );
因此,在修改这些功能后,如下错误已修复
THREE_DECL Vector3 getColumnX() const;
THREE_DECL Vector3 getColumnY() const;
THREE_DECL Vector3 getColumnZ() const;
THREE_DECL Matrix4& setPosition( const Vector3& v );
基于三个/config.hpp在使用仅标头版本的情况下THREE_DECL
定义为inline
.
我不明白的是
- 为什么即使在不同的编译单元中包含相同的头文件,也会出现多个定义错误?
- 添加
THREE_DECL
对于解决这些问题的意义是什么?
任何帮助将不胜感激。