我自己用VC6.0编写了一个基于MFC的静态库,需要在没有MFC的开发环境下调用,因此采用了User MFC in a Static Library的方式编译,编译后使用无MFC环境的开发工具来调用发现:在release环境下可以正常运行,但在debug模式下却无法正常运行,这样无法进行单步调试。后来使用VC6.0的MFC对话框应用程序中调用该静态库,用用程序同样采用User MFC in a Static Library的方式编译,在Release模式下可以运行,但是在debug调试模式下运行到
CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
ASSERT(m_pDocManager != NULL);
return m_pDocManager->OpenDocumentFile(lpszFileName);
}
时报错,其中lpszFileName为空,pDocManager 为NULL。
然后我采用User MFC in a Share DLL的方式编译静态库,调用时选择User MFC in a Share DLL模式编译应用程序,运行正常。
最后我尝试将编写了一个动态库,同样按上面的方式调用,无论Debug模式还是Release模式都可以正常调用。
但我现在需要的是静态库,并且需要运行在Debug模式下,为什么会出现这样的情况?
CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
ASSERT(m_pDocManager != NULL);
return m_pDocManager->OpenDocumentFile(lpszFileName);
}
时报错,其中lpszFileName为空,pDocManager 为NULL。
然后我采用User MFC in a Share DLL的方式编译静态库,调用时选择User MFC in a Share DLL模式编译应用程序,运行正常。
最后我尝试将编写了一个动态库,同样按上面的方式调用,无论Debug模式还是Release模式都可以正常调用。
但我现在需要的是静态库,并且需要运行在Debug模式下,为什么会出现这样的情况?
个人觉得你可以试试重新安装一下VC,可能是你的静态库出问题了,重新安装一下让它覆盖原来旧的DLL试试。
这样ASSERT等就用不了,rlease下又把它优化掉了,所以可以运行
// or project specific include files that are used frequently, but
// are changed infrequently
//#if !defined(AFX_STDAFX_H__E168FC92_2BF3_481D_9BDC_B43C23EE8A51__INCLUDED_)
#define AFX_STDAFX_H__E168FC92_2BF3_481D_9BDC_B43C23EE8A51__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers#include <afx.h>
#include <afxwin.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__E168FC92_2BF3_481D_9BDC_B43C23EE8A51__INCLUDED_)
SDK.cpp文件如下:
#include "stdafx.h"
#include "SDK.h"int WINAPI TestLib(int nTest)
{
nTest++;
return nTest;
}
SDK.h文件如下:
int WINAPI TestLib(int nTest);
stdafx.h文件如6楼
stdafx.cpp文件如下:
#include "stdafx.h"
调用如下:
void CTestLibDlg::OnButtonTest()
{
// TODO: Add your control notification handler code here
int nInput=2;
int nRet=TestLib(nInput);
CString strRet=_T("");
strRet.Format("Return:%d",nRet);
MessageBox(strRet);
}
试试编出两个不同版本的静态库,然后再根据编译环境链接相应的版本库
这样试试看。