C++ 在导出其他类、结构和联合时,不允许在一个特定联合处定义dllimport函数的错误

C++ 在导出其他类、结构和联合时,不允许在一个特定联合处定义dllimport函数的错误,c++,dllimport,dllexport,C++,Dllimport,Dllexport,我正在开发一个游戏引擎,并尝试导出一个联盟,但不知何故一直出现以下错误: 不允许定义dllimport函数 现在我知道这个错误是什么意思了,以前也解决过,但在这种情况下,我似乎找不到答案 我声明dllexport/dllimport如下: // Core.h #ifdef CH_PLATFORM_WINDOWS #ifdef CH_BUILD_DLL #define CH_API __declspec(dllexport) #else #define CH_API __declspec(d

我正在开发一个游戏引擎,并尝试导出一个联盟,但不知何故一直出现以下错误:

不允许定义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
template union CH_API Quaternion<float>;
template union CH_API Quaternion<int>;
template union CH_API Quaternion<double>;
我已经在多个地方使用了该宏,这些类、结构和联合按其应有的方式导出/导入,例如:

// 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_
但不知何故,联合四元数不会被导出,即使它与 上面的向量4并集

// 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] }
    {
    }
}
我没有包括所有的实现,因为它有很多行,如果需要更多,请在评论中告诉我。另外,我在一个.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;
}
这仍然会导致错误


不知何故,Chu API宏不断扩展到dllimportis,语法突出显示也显示了这一点,而Vector4联合体则不是这样。

正如评论中所说,联合体不需要通过删除它工作的Chu API宏来修饰dllexport。我仍然导出模板专门化

template<typename T>
union CH_API Quaternion
上述代码需要更改为:

template<typename T>
union Quaternion
然后我可以像这样导出模板专门化:

// 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
template union CH_API Quaternion<float>;
template union CH_API Quaternion<int>;
template union CH_API Quaternion<double>;

我还没有找到为什么Chu API会在这种特定情况下扩展到dllimport,以及使用Vector4联合扩展到dllexport,如果我找到了原因,我可能会更新我的答案。

我们不知道编译器,也不知道您在构建库或构建客户机代码时是否看到错误。但简而言之,这一切都不应该用dllimport/export来装饰。模板没有链接,客户机代码包含.h文件以获取所需的一切。“你也许可以导出专业知识。@汉帕桑你说得对,工会不需要装饰,我仍然可以导出专业知识。”。如果我拆下装饰,它就会工作。奇怪的是,在同一个文件中,在同一个include-guard中,一个宏可以同时扩展到dllexport和dllimport,但它对整个文件或库的值不应该相同吗?