Link to home
Start Free TrialLog in
Avatar of hari4130
hari4130

asked on

LNK2005 Error

I tried to Convert my MFC Project to .NET and I am getting the following linker errors

libcimtd.lib(ifstream.obj) : error LNK2005: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPAXHPBDH@Z) already defined in nafxcwd.lib(afxmem.obj
libcimtd.lib(ofstream.obj) : error LNK2005: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPAXHPBDH@Z) already defined in nafxcwd.lib(afxmem.obj)

libcimtd.lib(ifstream.obj) : warning LNK4006: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPAXHPBDH@Z) already defined in nafxcwd.lib(afxmem.obj); second definition ignored
libcimtd.lib(ofstream.obj) : warning LNK4006: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPAXHPBDH@Z) already defined in nafxcwd.lib(afxmem.obj); second definition ignored

I found out that the function delete is defined in the CRT library(libcimtd.lib) and MFC library(nafxcwd.lib) as well. Thats the problem.

I tried to follow the fix by microsoft
http://support.microsoft.com/default.aspx?scid=kb;en-us;q148652

(vs .net 2002)
1) In the Libraries to Ignore box, I inserted nafxcwd.lib libcimtd.lib
2) In the Additional Dependency, i added the same libs

This does not solve the problem. It gives the same errors.

Then somewhere i found another way to fix
1) go to project properties->General->Use of MFC option->
 I chose the option "Use MFC in a Shared DLL" instead of  "Use MFC in a Static Library"

This allowed me to compile and link with no errors, but when i try to debug the application i run into some wierd errors. I realized its becuase i chose the option  "Use MFC in a Shared DLL"

I tried another option
i tried to #include <afx.h> at the beginning of all my files. This did not help me either.

Please give me suggestions on How I can solve this

Hari
Avatar of jkr
jkr
Flag of Germany image

>> I chose the option "Use MFC in a Shared DLL" instead of  "Use MFC in a Static Library"

That'S the way it should be.

>>This allowed me to compile and link with no errors, but when i try to debug the application i run into some wierd
>>errors

What kind of errors?
Avatar of hari4130
hari4130

ASKER


In my Application:- When i put a break point at one of the dialogs, it doesn't go there. It gives "Debug Assertion Failed".

On the VS 6.0, it is set up as "Use MFC in a Static Library" and it works perfectly fine. Only when i migrated to .NET , I am getting these Linker Issues


SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>It gives "Debug Assertion Failed".

Where and for what reason? The debugger will stop at the line that causes the assertion and you'll be able to check the call stack then.
its causing an Unhandled Exception at

dlg.DoModal();

Unhandled exception at 0x7c253fd4 (mfc70d.dll) in Resolve.exe: User breakpoint.
The thread 'Win32 Thread' (0x668) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0x530) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0x9a4) has exited with code 3 (0x3).
The thread 'Win32 Thread' (0x6ec) has exited with code 3 (0x3).
Detected memory leaks!
Dumping objects ->
{85} normal block at 0x01455670, 91 bytes long.
 Data: <  .|J   J       > 10 FF 2E 7C 4A 00 00 00 4A 00 00 00 01 00 00 00
>> its causing an Unhandled Exception at
>>dlg.DoModal();

At which line? BTW, the above is not a call stack.
Is there a kind of fix where i can use the option "Use MFC in a Static Library" ?

In the Release version it works fine and does not give any linker errors
>>In the Release version it works fine and does not give any linker errors

... which does not necessarily mean that everything is OK.
>mfc70d.dll!CDialog::DoModal()  Line 488      C++
 SensorLink40.dll!CExtModel::LoadCase(HWND__ * hWnd=0x002f0668, _tErrorReturn * pstError=0x012842f4)  Line 468      C++SensorLink40.dll!ExtCmd(void * pAppObjInstance=0x01cb52a8, HWND__ * hWnd=0x002f0668, int iCmd=3, void * pvData=0x00000000, unsigned long dwData=0, _tErrorReturn * pstError=0x012842f4)  Line 131 + 0x13      C++
 Resolve.exe!0042e92f()       
 Resolve.exe!004311e1()       
 Resolve.exe!0043ad4f()       
 Resolve.exe!0043a8c5()       
 Resolve.exe!005cb7c1()       
 Resolve.exe!004e110b()       
 kernel32.dll!77e7d28e()       
I have sent the Call Stack. I am not able to determine the exact cause of the problem
What is in your dlgcore.cpp at line 488 (that's a MFC file, but you should have it on your system)?
I am having the file in the path

C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC
C:\Program Files\Microsoft Visual Studio .NET\Vc7\atlmfc\src\mfc

Line 488:

INT_PTR CDialog::DoModal()
{
      // can be constructed with a resource template or InitModalIndirect
      ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
            m_lpDialogTemplate != NULL);

      // load resource as necessary
      LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
      HGLOBAL hDialogTemplate = m_hDialogTemplate;
      HINSTANCE hInst = AfxGetResourceHandle();
      if (m_lpszTemplateName != NULL)
      {
            hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
            HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
            hDialogTemplate = LoadResource(hInst, hResource);
      }
      if (hDialogTemplate != NULL)
            lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

      // return -1 in case of failure to load the dialog template resource
      if (lpDialogTemplate == NULL)
            return -1;




ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
m_lpszTemplateName is <bad pointer>
m_hDialogTemplate is NULL
m_lpDialogTemplate  IS NULL

-> it casues the problem at this point - > HINSTANCE hInst = AfxGetResourceHandle();
Do you think its an error with the code itself that i wrote that is causing this ?

Because if i choose the option  "Use MFC in a Shared DLL" on VS 6.0 it throws the same exception

However if i choose the option Use MFC in a Static Library", everything seems to be working fine

The problem is that it is not identifying the dialog resouce.See That the resources are properly included.Add The .rc file and recompile everything.
I tried to check and re-compile the resource file, but that does not solve the problem either.

Any more suggestions please..
Does anyone have any suggestions that i can try.

What is the difference between choosing:

"Use MFC in a Static Library" vs Shared DLL. Is it necessary that i should choose the shared DLL option ?

I have been trying to obtain a fix for the problem above for a long time and nothing seems to work
If you choose Mfc as a staticLibrary The resources of Mfc will be statically Linked to your application.

Use Mfc shared Dll will dynamically use it.
I created a dialog based application VC++6.0 and opened it in .Net environment.It compiled and working prfectly fine
It is working perfectly fine in both options.


I opened  .dsp in visual studio.net and works fine.
If you put some piece of code  i will fix it.
I got it fixed. i wanted to share the solution

..It looks like you are using old VC6 iostream library. Take a look on this topic http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_c_run.2d.time_debug_libraries.asp
It describes how to switch you code to use new library, so you won't need to link to libcimtd.lib. Only libcmtd.lib and nafxcwd.lib should be on the linker's command line. Also, these libraries are included by linker by default, so unless you are using /nodefault linker switch, you don't need to specify these libraries on the command line. Then follow instructions on that support page that you have specified in your post. This should resolve all problems. Also afx.h should be included in the top of stdafx.h, no need to included it to each file...    by Nikola Dudar