将内联数学库与预编译头文件结合使用时发生链接错误
Link Error when using inline math library in combination with precompiled header file
我今天遇到了一个相当奇怪的问题。我有一个针对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
显式定义。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- 多态性和功能结合
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 与构造函数中rvalue结合的非const lvalue有关的错误
- 结合基于异常和错误代码的类实现
- 编译错误 将模板编程与特征C++库结合使用
- libcurl与curlpp结合使用时会出现编译错误
- 与unique_ptr相结合的升压ptr_vector错误
- Getline()结合cin.ignore()不存储变量?(错误)
- 将内联数学库与预编译头文件结合使用时发生链接错误