我正在开发一个游戏引擎,并试图导出一个联盟,但是不知怎么地,我仍然得到了以下错误:
不允许
定义dllimport函数
现在我知道这个错误意味着什么,并且已经解决了它,但是在这种情况下,我似乎找不到答案。
我声明如下:
// Core.h
#ifdef CH_PLATFORM_WINDOWS
#ifdef CH_BUILD_DLL
#define CH_API __declspec(dllexport)
#else
#define CH_API __declspec(dllimport)
#endif // CH_BUILD_DLL
#else
#error Cheetah currently only supports windows!
#endif // CH_PLATFORM_WINDOWS我已经在多个位置上使用了这个宏,并且这些类、结构和联合按它们应该的方式导出/导入,例如:
// Vector4.h
#ifndef CHEETAH_ENGINE_MATH_VECTOR4_H_
#define CHEETAH_ENGINE_MATH_VECTOR4_H_
#include "Core/Core.h"
#include "Vector3.h"
namespace cheetah
{
template<typename T>
union CH_API Vector4
{
inline Vector4();
inline Vector4(const T& fill);
inline Vector4(const T fill[4]);
inline Vector4(const Vector3<T>& fill, const T& w);
inline Vector4(const T& x, const T& y, const T& z, const T& w);
struct
{
T x, y, z, w;
};
inline const T* get() const;
inline T magnitude() const;
inline void operator *= (const T& rhs);
inline void operator += (const T& rhs);
inline void operator -= (const T& rhs);
inline void operator /= (const T& rhs);
inline Vector4<T> operator + (const Vector4<T>& rhs) const;
inline Vector4<T> operator - (const Vector4<T>& rhs) const;
inline T operator * (const Vector4<T>& rhs) const;
private:
struct
{
T m_data[4];
};
};
template union CH_API Vector4<float>;
template union CH_API Vector4<int>;
template union CH_API Vector4<double>;
using Vector4f = Vector4<float>;
using Vector4i = Vector4<int>;
using Vector4d = Vector4<double>;
}
#include "Vector4.inl"
#endif // !CHEETAH_ENGINE_MATH_VECTOR_H_但不知何故,联合四元数并没有得到出口,即使它是非常相同的Vector4联盟上面。
// Quaternion.h
#ifndef CHEETAH_CORE_MATH_QUATERNION_H_
#define CHEETAH_CORE_MATH_QUATERNION_H_
#include "Vector4.h"
#include "Vector3.h"
#include "Mat4x4.h"
#include<math.h>
#include "Core/Core.h"
namespace cheetah
{
template<typename T>
union CH_API Quaternion
{
public:
Quaternion();
Quaternion(const T& axisX, const T& axisY, const T& axisZ, const T& degrees);
Quaternion(const Vector3<T>& axis, const T& degrees);
Quaternion(const T fill[4]);
struct
{
T axisX, axisY, axisZ, degrees;
};
inline const T* get() const;
inline Mat4x4<T> getMatrix() const;
inline void normalize();
inline Quaternion<T> normalize(const Quaternion<T>& vector) const;
inline void operator *= (const T& rhs);
inline void operator += (const T& rhs);
inline void operator -= (const T& rhs);
inline void operator /= (const T& rhs);
inline Quaternion<T> operator + (const Vector4<T>& rhs) const;
inline Quaternion<T> operator - (const Vector4<T>& rhs) const;
inline T operator * (const Vector4<T>& rhs) const;
private:
struct
{
T m_data[4];
};
};
template union CH_API Quaternion<float>;
template union CH_API Quaternion<int>;
template union CH_API Quaternion<double>;
using Quaternionf = Quaternion<float>;
using Quaternioni = Quaternion<int>;
using Quaterniond = Quaternion<double>;
}
#include "Quaternion.inl"
#endif // !CHEETAH_CORE_MATH_QUATERNION_H_此错误将抛到包含所有四元数实现的实现文件Quaternion.inl中的四元数联合的所有构造函数上。
// Quaternion.inl
namespace cheetah
{
template<typename T>
inline Quaternion<T>::Quaternion()
: m_data{ 0, 0, 0, 0 }
{
}
template<typename T>
inline Quaternion<T>::Quaternion(const T& axisX, const T& axisY, const T& axisZ, const T&
degrees)
: m_data{ axisX, axisY, axisZ, degrees }
{
}
template<typename T>
inline Quaternion<T>::Quaternion(const Vector3<T>& axis, const T& degrees)
: m_data{ axis.x, axis.y, axis.z, degrees }
{
}
template<typename T>
inline Quaternion<T>::Quaternion(const T fill[4])
: m_data{ fill[0], fill[1], fill[2], fill[3] }
{
}
}我没有包括所有的实现,因为它有很多行,如果需要更多的话,请在注释中告诉我。另外,我在a.cpp文件中转发了我想要导出的两个联合的专门化。
我看不出vector4和四元数联合之间有什么区别,以及为什么Vector4被导出,而四元数没有。
我尝试过的:
我试图从四元数的.inl文件中删除所有类专门化方法,因为这是与Vector4联盟的少数几个不同之一,四元数有一些专门化成员,如下所示:
template<>
inline void Quaternion<float>::normalize()
{
const float n = 1.0f / sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ + degrees * degrees);
m_data[0] *= n;
m_data[1] *= n;
m_data[2] *= n;
m_data[3] *= n;
}这仍然会导致错误。
不知怎么的,CH_API宏一直被扩展到dllimport(语法高亮显示中也显示了),而Vector4联合则不是这样。
发布于 2020-01-13 08:14:22
正如评论中所说的,工会不需要通过删除它工作的CH_API宏来装饰dllexport。我仍然导出模板的专门化。
template<typename T>
union CH_API Quaternion需要将上述代码更改为:
template<typename T>
union Quaternion然后,我可以导出模板的专门化如下:
template union CH_API Quaternion<float>;
template union CH_API Quaternion<int>;
template union CH_API Quaternion<double>;但是,在这种特殊情况下,我还无法找到为什么将CH_API扩展到dllimport,并且无法与Vector4联合进行dllexport,如果我知道为什么会发生这种情况,我可能会更新我的答案。
https://stackoverflow.com/questions/59707992
复制相似问题