错误定义一个特定联合时不允许dllimport函数的定义,而其他类,结构和联合则按应有的方式导出

问题描述 投票:0回答:1

我正在使用游戏引擎,并尝试导出联合,但是以某种方式不断出现以下错误:

不允许dllimport函数的定义

现在,我知道此错误的含义并且之前已经解决了,但是在这种情况下,我似乎找不到答案。

我这样声明dllexport / 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的所有实现的实现文件Quaternion.inl中的Quaternion联合的所有构造函数上引发。

// 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被导出但四元数没有导出。

我尝试过的事情:

我试图从Quaternion的.inl文件中删除所有类的专业化方法,因为那是与Vector4联合的少数差异之一,The Quaternion具有如下某些专业化成员:

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联合不是这种情况。

c++ dllimport dllexport
1个回答
0
投票
如评论中所述,通过删除它起作用的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,如果我找出原因,我可能会更新我的答案。
© www.soinside.com 2019 - 2024. All rights reserved.