将内联数学库与预编译头文件结合使用时发生链接错误

Link Error when using inline math library in combination with precompiled header file

本文关键字:结合 错误 链接 文件 编译      更新时间:2023-10-16

我今天遇到了一个相当奇怪的问题。我有一个针对SSE优化的数学库,因此几乎所有功能都声明为内联。为了简化起见,我将只使用一个类来解释这个问题,Vector3:

Vector3在Vector3.h中声明,类似于以下内容:

#ifndef VIRTUS_VECTOR3_H
#define VIRTUS_VECTOR3_H
#ifndef VEC3INLINE
  #if(_DEBUG)
    #define VEC3INLINE inline
  #else
    #define VEC3INLINE __forceinline
  #endif
#endif
namespace Virtus {
struct Vector3
{
  union
  {
    struct { f32 x,y,z; };
    f32 v[3];
  };
  Vector3(void);
  Vector3(const Vector3& rhs);
  Vector3(f32 xx, f32 yy, f32 zz);
  VEC3INLINE Vector3& operator=(const Vector3& rhs);
  VEC3INLINE Vector3 operator+(f32 rhs) const;
  VEC3INLINE Vector3 operator-(f32 rhs) const;
  VEC3INLINE Vector3 operator*(f32 rhs) const;
  VEC3INLINE Vector3 operator/(f32 rhs) const;
  VEC3INLINE Vector3& operator+=(f32 rhs);
  VEC3INLINE Vector3& operator-=(f32 rhs);
...
#include "vector3.inl"
#endif

在vector3.inl中,我继续定义的所有功能

namespace Virtus {
Vector3::Vector3(void)
  : x(0.0f), y(0.0f), z(0.0f)
{
}
Vector3::Vector3(const Vector3& rhs)
  : x(rhs.x), y(rhs.y), z(rhs.z)
{
}
Vector3::Vector3(f32 xx, f32 yy, f32 zz)
  : x(xx), y(yy), z(zz)
{
    }
VEC3INLINE Vector3& Vector3::operator=(const Vector3& rhs)
{
  memcpy(v, rhs.v, sizeof(v));
  return *this;
}
    ...

然后,我将所有的数学对象都包含在一个名为math.h 的文件中

#ifndef VIRTUS_MATH_H
#define VIRTUS_MATH_H
#include "vector2.h"
#include "vector3.h"
#include "vector4.h"
#include "matrix4.h"
#include "primesearch.h"
namespace Virtus
{
  class MathException
  {
  public:
    enum ErrorCode
    {
      PRIME_SEARCH_INVALID_ELEMENTS,
      PRIME_SEARCH_UNSUFFNUM_PRIMES
    };
    MathException(ErrorCode code) : m_Error(code) {}
    ErrorCode What(void) const { return m_Error; }
  private:
    ErrorCode m_Error;
  };
} // namespace virtus
#endif // Include Guard

math.h包含在我的预编译头文件(precompiled.h,又名stdafx.h)中

我使用的是Visual Studio 2008,它应该会自动从生成过程中排除.inl文件。

这是我得到的链接器错误:

错误1错误LNK2005:"公共:__thiscall Virtus::Vector3::Vector3(void)"(??0Vector3@Virtus@@QAE@XZ)已经定义于预编译.obj main.obj Virtus

我尝试了几乎所有可以想象的方法来修复这个问题,比如手动从构建中排除所有inl文件;在预编译文件中不包括math.h,而只包括在我需要它的地方(在这种情况下,我会得到类似的已经定义的链接器错误);从inl扩展改为cpp扩展,等等。我唯一能解决这个问题的方法是使用#pragma一次,而不是包含保护。所以我现在最好的猜测是,这与预编译的头文件和include guard的组合有关,但我真的不确定如何解决这个问题。

非常感谢您的帮助!

vector3.inl中的每个定义都需要用inline显式定义。