■■■郁闷,MFC 下使用 greta, 连接错误

SGUav 2007-12-26 12:36:09
想使用正则表达式,boost 在MFC 下使用编译时好时坏,有点诡异,心想使用微软自己的 Greta 可能会好些

找来了 greta for vc6,在 win32 控制台程序下使用没问题

到了 MFC 下,在一个对话框程序中试着使用 greta,编译没有问题,无论是 VC6 自己的编译器还是 ICC8,但是连接出现多个错误。基本上全部是 error lnk2005,标识符函数名什么的已经被定义

俺估计是不是 greta 在内部实现或是文件包含中,在命名上与MFC库有冲突,于是俺在 greta 的基本上每个文件开头处都使用了using namespace std,但是没有丝毫效果。

请教大家究竟是怎么回事,又没有解决的办法?

谢谢!
...全文
373 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
SGUav 2007-12-28
  • 打赏
  • 举报
回复
果然如 jiangsheng 所言,是连接使用的CRT库版本不一致所导致的问题。

解决办法:

Project setting -> c/c++ -> category 选择 code generation ->user run-time library

选择相同的库即可

SGUav 2007-12-26
  • 打赏
  • 举报
回复
感谢楼上二位,特别是 jiangsheng

我找到一篇文章,先照方抓药试一试,有问题再来请教


http://www.cppblog.com/fwxjj/archive/2007/09/21/32637.aspx


在 Visual C++ 中以错误的顺序链接 CRT 库和 MFC 库时出现 LNK2005 错误症状
当 C 运行时 (CRT) 库和 Microsoft 基础类 (MFC) 库的链接顺序有误时,可能会出现以下 LNK2005 错误之一:
nafxcwd.lib(afxmem.obj) :error LNK2005:
"void * __cdecl operator new(unsigned int)"(??2@YAPAXI@Z) already
defined in LIBCMTD.lib(new.obj)
nafxcwd.lib(afxmem.obj) :error LNK2005:
"void __cdecl operator delete(void *)"(??3@YAXPAX@Z) already defined
in LIBCMTD.lib(dbgnew.obj)
nafxcwd.lib(afxmem.obj) :error LNK2005:
"void * __cdecl operator new(unsigned int,int,char const *,int)"
(??2@YAPAXIHPBDH@Z) already defined in LIBCMTD.lib(dbgnew.obj)
mfcs40d.lib(dllmodul.obj):error LNK2005:_DllMain@12 already defined in
MSVCRTD.LIB (dllmain.obj)
mfcs42d.lib(dllmodul.obj):error LNK2005:_DllMain@12 already defined in
msvcrtd.lib(dllmain.obj)
原因
CRT 库对 new、delete 和 DllMain 函数使用弱外部链接。MFC 库也包含 new、delete 和 DllMain 函数。这些函数要求先链接 MFC 库,然后再链接 CRT 库。


解决方案
该问题有两种解决方法。第一种方法是强制链接器按照正确的顺序链接库。第二种方法是由您亲自查找导致问题的模块并纠正它。

注意:下列步骤基于 Visual C++ 6.0 进行。
解决方案一:强制链接器按照正确的顺序链接库
1. 在“项目”菜单上,单击“设置”。
2. 在“项目设置”对话框的“以下项目的设置”视图中,单击以选中出现链接错误的项目配置。
3. 在“链接”选项卡上,单击以选中“类别”组合框中的“输入”。
4. 在“忽略库”框中,插入库名(例如,Nafxcwd.lib;Libcmtd.lib)。

注意:等效的链接器命令行是:/NOD:<library name>。
5. 在“对象/库模块”框中,插入库名。必须确保这些库按顺序列出,而且是行中的前两个库(例如,Nafxcwd.lib 和 Libcmtd.lib)。
要在 Visual C++ .NET 中设置该选项,请阅读“设置 Visual C++ 项目属性”联机帮助主题。
解决方案二:找到并纠正出现问题的模块
要查看当前的库链接顺序,请按照下列步骤操作: 1. 在“项目”菜单上,单击“设置”。
2. 在“项目设置”对话框的“以下项目的设置”视图中,单击以选中出现链接错误的项目配置。
3. 在“链接”选项卡上的“项目选项”框中键入 /verbose:lib。
4. 重新生成项目。在链接过程中,这些库将在输出窗口中列出。

状态
这种现象是设计导致的。
更多信息
使用 MFC 库时,务必先链接它们,然后再链接 CRT 库。这可以通过确保项目中的每个文件都首先包含 Msdev\Mfc\Include\Afx.h 来完成。直接包含 (#include <Afx.h>) 或间接包含 (#include <Stdafx.h>) 都可以。Afx.h 包含文件会通过使用 #pragma comment (lib,"<libname>") 指令来强制采用库的正确顺序。

如果源文件的扩展名为 .c,或者该文件的扩展名为 .cpp 但不使用 MFC,则可以创建一个较小的头文件 (Forcelib.h) 并将其放在模块的顶端。这个新的头文件可确保按照正确的顺序搜索库。

Visual C++ 不包含该头文件。要创建此文件,请按照下列步骤操作: 1. 打开 Msdev\Mfc\Include\Afx.h。
2. 选定 #ifndef _AFX_NOFORCE_LIBS 和 #endif //!_AFX_NOFORCE_LIBS 之间的行。
3. 将选定部分复制到 Windows 剪贴板。
4. 创建一个新文本文件。
5. 将剪贴板的内容粘贴到这个新文件中。
6. 将该文件另存为 Msdev\Mfc\Include\Forcelib.h。

在 Visual C++ .NET 中重现问题的步骤
1. 启动 Microsoft Visual Studio .NET。
2. 在“文件”菜单上,指向“新建”,然后单击“项目”。
3. 单击“项目类型”下的“Visual C++ 项目”,然后单击“模板”下的“MFC 应用程序”。
4. 在“名称”文本框中,键入 Q148652。
5. 在“位置”文本框中,键入 C:\Test,然后单击“确定”。
6. 在“MFC 应用程序向导”对话框中,单击“应用程序类型”。
7. 单击“应用程序类型”下的“基于对话框”,然后单击“MFC 的使用”下的“在静态库中使用 MFC”。
8. 单击“完成”。
9. 在“解决方案资源管理器”中,选择“源文件”下的全部三个 .cpp 文件。
10. 右键单击三个选定的文件,然后单击“删除”。
11. 右键单击“源文件”,指向“添加”,然后单击“添加新项”。
12. 单击“模板”下的“C++ 文件”。在“名称”文本框中,键入 Aa。单击“打开”。
13. 将以下代码粘贴到 Aa.cpp 中:
int test(){new int; return 1;}

14. 右键单击“源文件”,指向“添加”,然后单击“添加现有项”。
15. 选择以下文件: • Q148652.cpp
• Q148652Dlg.cpp
• stdafx.cpp

16. 单击“打开”。
17. 您在第 15 步中选择的文件将出现在“源文件”下。
18. 选择“源文件”下的全部四个 .cpp 文件。
19. 右键单击选定的四个 .cpp 文件,然后单击“属性”。
20. 展开“配置属性”,然后展开“C/C++”。
21. 单击“预编译头”。
22. 将“创建/使用预编译头”属性设置为“不使用预编译头”。单击“确定”。
23. 在“生成”菜单上,单击“重新生成解决方案”。
laolaoliu2002 2007-12-26
  • 打赏
  • 举报
回复
没用过,不过http://www.vckbase.com/document/viewdoc/?id=1138或许对你有帮助
蒋晟 2007-12-26
  • 打赏
  • 举报
回复
一般是连接了使用不同版本的CRT的库
确认你连接的所有的库都使用同一个版本的CRT
SGUav 2007-12-26
  • 打赏
  • 举报
回复
更正上贴:

一编译连接,编译没有错误,但有二十几个警告,但是连接时就出现了上述错误。
SGUav 2007-12-26
  • 打赏
  • 举报
回复
连接错误信息是这样的:


--------------------Configuration: Main - Win32 Debug--------------------
Compiling resources...
Compiling...
Main.cpp
MainDlg.cpp
StdAfx.cpp
Linking...
xilink6: executing 'C:\PROGRA~1\MICROS~3\VC98\Bin\link.exe'
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: virtual __thiscall std::logic_error::~logic_error(void)" (??1logic_error@std@@UAE@XZ) already defined in libcpmtd.lib(string.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::logic_error::logic_error(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0logic_error@std@@QAE@ABV?$basic_string@DU?$char_trait
s@D@std@@V?$allocator@D@2@@1@@Z) already defined in libcpmtd.lib(string.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::logic_error::logic_error(class std::logic_error const &)" (??0logic_error@std@@QAE@ABV01@@Z) already defined in libcpmtd.lib(string.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_strin
g@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in MainDlg.obj
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::allocator<char
> const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV?$allocator@D@1@@Z) already defined in MainDlg.obj
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<c
har,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in MainDlg.obj
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: char * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::end(void)" (?end@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEPADXZ) already
defined in libcpmtd.lib(iostream.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: char * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::begin(void)" (?begin@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEPADXZ) alr
eady defined in libcpmtd.lib(iostream.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *,class std::a
llocator<char> const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBDABV?$allocator@D@1@@Z) already defined in MainDlg.obj
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) already defined in libcpmtd.lib(xlock.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::_Lockit::_Lockit(void)" (??0_Lockit@std@@QAE@XZ) already defined in libcpmtd.lib(xlock.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::bad_alloc::bad_alloc(char const *)" (??0bad_alloc@std@@QAE@PBD@Z) already defined in libcpmtd.lib(nomemory.obj)
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: static void __cdecl std::char_traits<char>::assign(char &,char const &)" (?assign@?$char_traits@D@std@@SAXAADABD@Z) already defined in MainDlg.obj
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: unsigned int __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::size(void)const " (?size@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBE
IXZ) already defined in MainDlg.obj
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::erase(unsign
ed int,unsigned int)" (?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@II@Z) already defined in MainDlg.obj
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __CrtDbgReport already defined in libcmtd.lib(dbgrpt.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: "public: int __thiscall type_info::operator==(class type_info const &)const " (??8type_info@@QBEHABV0@@Z) already defined in libcmtd.lib(typinfo.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __isctype already defined in libcmtd.lib(isctype.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _toupper already defined in libcmtd.lib(toupper.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _tolower already defined in libcmtd.lib(tolower.obj)
LINK : warning LNK4098: defaultlib "MSVCRTD" conflicts with use of other libs; use /NODEFAULTLIB:library
Debug/Main.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing xilink6.exe.

Main.exe - 21 error(s), 3 warning(s)







我设置了一个WorkSpace TryGreta,将Greta提供的6个文件作为TryGreta下的一个静态Lib Project,为它设置了MFC支持以及预编译头,并且让它依赖基于对话框的 Project Main。在该对话框建立了一个对按钮的消息相应函数,在这个函数中有以下代码:

match_results results;
rpattern pat( "\\$(\\d+)(\\.(\\d\\d))?" );

在对话框类实现文件的开头处加入以下代码:

#include <iostream>
#include <string>
#include "..\gre\regexpr2.h"

using namespace std;
using namespace regex;


一编译连接,编译没有错误,但有二十几个错误。但连接时就出现了上述错误。





蒋晟 2007-12-26
  • 打赏
  • 举报
回复
什么函数被定义?
yxwsbobo 2007-12-26
  • 打赏
  • 举报
回复
JF

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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