【发布时间】:2013-03-16 14:45:22
【问题描述】:
关于 .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 上定义的static struct Mouse mouse 我得到链接错误
我通过在这个文件上重新定义它来解决我的问题
//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 项目正在增长,上面有静态变量。
【问题讨论】:
-
但是您是否与
SWC_Base.cpp链接? -
是的,除了
mouse让我遇到链接问题。我花了一天时间才弄明白。 -
这不是实际代码。我什至不会编译。
-
@harper 这是一千行代码。如果我把它放在这里,每个人都会感兴趣吗?
-
您对一个现已删除的答案发表了评论,称您“使用宏正确链接它”。你是什么意思?宏是预处理器的东西,与链接器无关。请创建一个SSCCE 并向我们展示。
标签: c++ visual-c++ dll static linker