求助动态链接库链接问题!

shils80 2006-05-10 09:57:41
有一个类声明如下:
class DLLOPT DefaultLog {
public:
DefaultLog() { }
~DefaultLog() { }

/**
* Initialize the default logger with the given logging implementation.
*
* @param logger
* an AgentLog instance to be used as default logger. A previously
* set logger will be deleted.
*/
static void init(AgentLog* logger)
{ if (instance) delete instance; instance = logger; }

/**
* Return the default logger.
*
* @return
* a pointer to an AgentLog instance.
*/
static AgentLog* log()
{ if (!instance) init(new AgentLogImpl()); return instance; }

/**
* Create a new log entry or reuse an existing one.
*
* @param type
* the type of the log entry as bitwise or of log class and level.
*/
static void create_log_entry(unsigned char t)
{ if (!entry) { entry = log()->create_log_entry(t); entry->init();} }

/**
* Return the current log entry. If there is none, an ERROR_LOG entry
* with level 1 will be created.
*
* @return
* a pointer to a LogEntry instance.
*/
static LogEntry* log_entry()
{ if (!entry) create_log_entry(ERROR_LOG | 1); return entry; }

/**
* Delete current log entry.
*/
static void delete_log_entry()
{ if (entry) delete entry; entry = 0; }

protected:

static AgentLog* instance;
static LogEntry* entry;
};

我和其他一些文件编译生成lib和dll,在使用过程中出现如下问题:
error LNK2001: unresolved external symbol "protected: static class AgentLog * DefaultLog::instance" (?instance@DefaultLog@@1PAVAgentLog@@A)
error LNK2001: unresolved external symbol "protected: static class LogEntry * DefaultLog::entry" (?entry@DefaultLog@@1PAVLogEntry@@A)
开始我怀疑没有正确导出这两个成员变量,于是我使用dumpbin导出dll发现里面有如下项:
2796 AEB 0043AC74 ?instance@DefaultLog@@1PAVAgentLog@@A
2127 84E 0043AC78 ?entry@DefaultLog@@1PAVLogEntry@@A
VC环境的设置应该没有问题,因为如果我在setting里去掉对应的lib文件,将会出现更多的链接错误。
不知道问题出在哪里,谢谢各位指点!
...全文
166 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
屋顶上的老猫 2006-05-10
  • 打赏
  • 举报
回复
模块定义不麻烦,考虑到可移植性这也是必须的!它只是规定导出成员的名字,名字粉碎在导出时就会发生,因为要保证面向对象中的重载性!
shils80 2006-05-10
  • 打赏
  • 举报
回复
名字粉碎?什么情况下会发生这个情况,如何避免呢?自己写.def很麻烦吧
屋顶上的老猫 2006-05-10
  • 打赏
  • 举报
回复
这是发生“名字粉碎”机制,使用.def模块定义!或者利用内部索引号调用!
shils80 2006-05-10
  • 打赏
  • 举报
回复
我想把这个类理解成全局函数和全局变量好了,只是通过类来封装罢了,一直找不到问题出在哪里,郁闷中.
shils80 2006-05-10
  • 打赏
  • 举报
回复
呵呵,这个类不是我写的,是开源的agent++里面的一个类,这两个静态变量有初始化,至于为什么是static,如果函数是static并且要访问成员变量,那成员变量必然是static了。
liu_jun_ivan 2006-05-10
  • 打赏
  • 举报
回复
这个问题有点...弱。你那类里面的两个静态变量需要在类的外面初始化,估计你没有做。而且看了你这个类,觉得很怪,怎么函数和成员变量都是static类型的?什么目的?
shils80 2006-05-10
  • 打赏
  • 举报
回复
DLLOPT就是_declspec(dllexport),如果这个有问题,我想连导出都会有问题吧。
sankt 2006-05-10
  • 打赏
  • 举报
回复
class _declspec(dllexport) class_name //导出类
{
 …
}
shils80 2006-05-10
  • 打赏
  • 举报
回复
我把它做成静态链接库,调用就没有问题了,搞不懂。
shils80 2006-05-10
  • 打赏
  • 举报
回复
我和下面的问题是类似的:
http://tech.cixiong.com/t/200407/16/0227105.html
shils80 2006-05-10
  • 打赏
  • 举报
回复
我的库是隐式连接,已经采用类导出了,而且我看到确实导出了该成员变量,我自己做了一个简单例子,在类中声明静态成员变量没有问题,为什么这个会有问题,难道动态链接库不能用静态的类型为类的成员变量?
屋顶上的老猫 2006-05-10
  • 打赏
  • 举报
回复
我还是不明白,我这个软件包要导出几千个函数及变量,我要是都写模块定义那岂不是很麻烦
//采用面向对象,那就直接进行类导出!

你的库是动态加载,还是隐式连接?

shils80 2006-05-10
  • 打赏
  • 举报
回复
我还是不明白,我这个软件包要导出几千个函数及变量,我要是都写模块定义那岂不是很麻烦,而且我没有涉及到跨平台,为什么会出现无法链接的问题呢?唉

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧