没有办法解决这个问题了吗?log4cplus在VS2005中的link错误,关于模板使用

thinkinnight 2007-03-31 10:36:54
昨天下载了一个log4cplus,这些工具类不想自己写了,用用现成的不是很好。不过首先发现最后一次修改还是2004年的,不管了,先下下来吧。
使用VS2005编译,出现几个编译错误,这个不难,google之,很快就找到了答案。
http://aspalliance.com/groups/microsoft_public_dotnet_languages_vc/ng-973873_VS2005_breaking_my_builds_.aspx

replace all occurrences of __value with something else: __value is recognized as a keyword by the VC2005 compiler (the authors of log4cpp should not have used it - all identifiers containing two consequetive underscores are reserved to the implementation).

For the specific error you're seeing, insert the following line of code into \include\log4cplus\helpers\stringhelper.h, line 123:
typedef std::output_iterator_tag iterator_category;

根据上面的建议修改之后,编译可以通过,但是发现一共16个工程,有一个工程无法build成功,有三个link错误。
错误内容:

1. main.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: class TestThread * __thiscall log4cplus::helpers::SharedObjectPtr<class TestThread>::operator->(void)const " (__imp_??C?$SharedObjectPtr@VTestThread@@@helpers@log4cplus@@QBEPAVTestThread@@XZ),该符号在函数 _main 中被引用

2. main.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __thiscall log4cplus::helpers::SharedObjectPtr<class TestThread>::`default constructor closure'(void)" (__imp_??_F?$SharedObjectPtr@VTestThread@@@helpers@log4cplus@@QAEXXZ),该符号在函数 _main 中被引用

3. main.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall log4cplus::helpers::SharedObjectPtr<class TestThread>::~SharedObjectPtr<class TestThread>(void)" (__imp_??1?$SharedObjectPtr@VTestThread@@@helpers@log4cplus@@QAE@XZ),该符号在函数 _main 中被引用
.\thread_test_Release/thread_test.exe : fatal error LNK1120: 3 个无法解析的外部命令

于是去thread_test工程下,里面就一个main.cxx
主要就是这一句:
第83行,
log4cplus::helpers::SharedObjectPtr<TestThread> threads[NUM_THREADS];

这里无法找到,再看生成的dll文件呢,于是dumpbin一下,发现其导出是这样的。
7 6 0001CD30 ??0?$SharedObjectPtr@VAbstractThread@thread@log4cplus@@@helpers@log4cplus@@QAE@PAVAbstractThread@thread@2@@Z

怎么会这样呢?
而且经过实验,发现VC6编译能够通过,但是通过dumpbin观察,其导出函数与VS2005很不同。

继续google,找到一位和我问题差不多的
http://www.eggheadcafe.com/forumarchives/vclanguage/Aug2005/post23130691.asp

看来就是因为模板不是real code,所以dll无法根据实际情况导出,这里的class TestThread只有exe知道,所以link的时候找不到,因为没有生成嘛。而VC6,是像这句话所说
Some compilers such as VC6 assume that all template code must have all possible instantiations in the ?translation unit that it is declared in, but there's no real justification

现在我的问题就是,如何做,使得这个代码可以编译通过呢?
还是因为C++本身的语法条件局限,根本就是一个无法解决的问题呢?

多谢多谢!

PS:贪心贪心,还想问问,除了log4cplus外,是否有其他log类。
已试用log4cpp,但是似乎工程都不全,有很多.vcproject都没有,就没有搞。而且也很旧了,是2005年的。
至于log4cxx,下载的0.9.7,看介绍说有不少bug,而0.9.8因为要大改还是啥的,还没出来。
有无其他推荐?
...全文
900 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinkinnight 2007-04-05
  • 打赏
  • 举报
回复
关键这不是我的代码,而是log4cplus的程序代码,它其他代码都可以运行正确,但是唯独这个测试thread的代码不行,当然,现在我还不需要使用到这部分,但是如果我要使用它来做log的话,肯定以后会遇到thread需要log的,所以我现在希望对这个了解得越多越好,如果实在不行,那只能抛弃这个log4cplus。

对于dll导出类,的确是非常不好,《windows核心编程》中也强调了,我个人编码也尽量注意不这样做,但是这些都是外部库。

对于模板,由于以前的编译器对模板支持之差,现在逐渐的变化得支持好,但是在这样的变化过程中,使得不知道哪些可以用,哪些不可以用,很苦恼。
taodm 2007-04-05
  • 打赏
  • 举报
回复
你在dll里导出类,而且是模板类的实例?
只要有可能,就别在dll导出类。
thinkinnight 2007-03-31
  • 打赏
  • 举报
回复
根据LZ的建议,将工程属性修改之后,还是不行
laiwusheng 2007-03-31
  • 打赏
  • 举报
回复
将工程属性|配置属性|C/C++|高级里的_cdecl改为_stdcall看看
thinkinnight 2007-03-31
  • 打赏
  • 举报
回复
附上log4cplus的下载地址:
http://sourceforge.net/project/showfiles.php?group_id=40830
使用的是1.0.2的版本
thinkinnight 2007-03-31
  • 打赏
  • 举报
回复
sorry,应该是ls的建议,还是link错误,而且link错误更多,无法解决

64,648

社区成员

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

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