首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dllimport函数的错误定义不允许在一个特定的联合中,而其他类、结构和联合则按它们应该导出的方式导出。

dllimport函数的错误定义不允许在一个特定的联合中,而其他类、结构和联合则按它们应该导出的方式导出。
EN

Stack Overflow用户
提问于 2020-01-12 20:48:56
回答 1查看 66关注 0票数 0

我正在开发一个游戏引擎,并试图导出一个联盟,但是不知怎么地,我仍然得到了以下错误:

不允许

定义dllimport函数

现在我知道这个错误意味着什么,并且已经解决了它,但是在这种情况下,我似乎找不到答案。

我声明如下:

代码语言:javascript
复制
// 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

我已经在多个位置上使用了这个宏,并且这些类、结构和联合按它们应该的方式导出/导入,例如:

代码语言:javascript
复制
// 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联盟上面。

代码语言:javascript
复制
// 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中的四元数联合的所有构造函数上。

代码语言:javascript
复制
// 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联盟的少数几个不同之一,四元数有一些专门化成员,如下所示:

代码语言:javascript
复制
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联合则不是这样。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-13 08:14:22

正如评论中所说的,工会不需要通过删除它工作的CH_API宏来装饰dllexport。我仍然导出模板的专门化。

代码语言:javascript
复制
template<typename T>
union CH_API Quaternion

需要将上述代码更改为:

代码语言:javascript
复制
template<typename T>
union Quaternion

然后,我可以导出模板的专门化如下:

代码语言:javascript
复制
template union CH_API Quaternion<float>;
template union CH_API Quaternion<int>;
template union CH_API Quaternion<double>;

但是,在这种特殊情况下,我还无法找到为什么将CH_API扩展到dllimport,并且无法与Vector4联合进行dllexport,如果我知道为什么会发生这种情况,我可能会更新我的答案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59707992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档