将存储类的问题定义为静态 [英] Linking problems with storage class defined as static

查看:141
本文介绍了将存储类的问题定义为静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.dll文件中

//SWC.h

#ifndef _SWC_
#    define _SWC_
#    define SWC_CALL __declspec(dllexport)
#else
#    define SWC_CALL __declspec(dllimport)
#endif

namespace SWC
{

    struct SWC_CALL Mouse
    {
        //interface
    };

    class SWC_CALL SWC_Base : public someClass1, public someClass2
    {

        static Mouse mouse;

    };

    //other classes goes here...
}

//SWC_Base.cpp
namespace SWC
{

    Mouse SWC_Base::mouse; //needed, to compile

    //other SWC_Base function definition

}

在.exe文件上

静态结构鼠标鼠标我定义在 SWC_Base 我得到链接错误

with the static struct Mouse mouse I defined on the SWC_Base I get linking errors

我通过在这个文件上重新定义它来解决我的问题

I solve my problem by redefining it again on this file

//main.cpp

#include "SWC.h"

#pragma comment (lib, "..\\SWC")

SWC::Mouse SWC::SWC_Base::mouse; //<- why do I need to redefine it again?

int main()
{
    //...

    return 0;

}

我已经在其.cpp上定义了SWC_Base :: mouse文件,为什么我需要在使用它的文件上重新定义它?我知道我可以有更多的问题,因为我的.dll项目正在增加静态变量。

I already define the SWC_Base::mouse on its .cpp file, why do I need to redefine it again on the file who uses it? I know I can have this more problems as my .dll project is growing with static variables on it.

推荐答案

将使用 __ declspec(dllimport)这个麻烦会消失:)

If your calling code will use __declspec (dllimport) this trouble will be gone :)

#ifdef EXPORTING_SWC
  #define SWC_CALL __declspec(dllexport)
#else
  #define SWC_CALL __declspec(dllimport)
#endif

这篇关于将存储类的问题定义为静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆