1

我正在用 C++ 编写一个 *dll 来连接数据库。我尝试在我的控制台应用程序中使用 afxdb.h,它工作正常。现在,我想在我的 *dll 中使用相同的代码。所以,我将 afxdb.h 添加到 stdafx.h 中,当我编译时给了我这个错误

mfcs42d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in JunkDLL.obj
mfcs42d.lib(dllmodul.obj) : warning LNK4006: _DllMain@12 already defined in JunkDLL.obj; second definition ignored
Creating library Debug/JunkDLL.lib and object Debug/JunkDLL.exp
Debug/JunkDLL.dll : fatal error LNK1169: one or more multiply defined symbols found

我的 stdafx.h 看起来像

#if !defined(AFX_STDAFX_H__123__INCLUDED_)
#define AFX_STDAFX_H__123__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "afxwin.h"
#include "afxext.h"
#include "afxdb.h"
// Insert your headers here
#define WIN32_LEAN_AND_MEAN// Exclude rarely-used stuff from Windows headers
#include "windows.h"
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the  previous line.
#endif // !defined(AFX_STDAFX_H__123__INCLUDED_)

谢谢你的帮助。

4

1 回答 1

1

您的 DLL静态链接到 MFC。在这种情况下,MFC 提供了自己的DllMain()入口点,而您的 DLL 不应提供。

您可以在CWinApp派生的单例InitInstance()方法中执行初始化,也可以将 DLL 动态链接到 MFC

于 2012-07-10T17:41:48.137 回答