Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - error LNK2005 in debug mode

I'm integrating 3rd party code into my MFC app under Visual Studio 2010.
When in Debug mode the following build error occurs:

1>LIBCMT.lib(invarg.obj) : error LNK2005: __initp_misc_invarg already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __call_reportfault already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __set_invalid_parameter_handler already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __get_invalid_parameter_handler already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: "void __cdecl _invoke_watson(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invoke_watson@@YAXPBG00II@Z) already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __invalid_parameter already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: "void __cdecl _invalid_parameter(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invalid_parameter@@YAXPBG00II@Z) already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: ___pInvalidArgHandler already defined in libcmtd.lib(invarg.obj)
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>D:\My Documents\Dev\MyProject\MyProject\Debug\MyProject.exe : fatal error LNK1169: one or more multiply defined symbols found

Using this advice I was able to complete the build in two ways:

  • In Release mode
  • In Debug mode using /FORCE:MULTIPLE as an additional linker command line option

In the second case (Debug mode) many warnings are still reported. If I also add /NODEFAULTLIB:LIBCMT most of them are gone.

What is the cause of this?
How can I solve this, instead of working around it?

like image 487
Jonathan Livni Avatar asked Oct 07 '10 20:10

Jonathan Livni


People also ask

How to ignore a specific library in lnk4098?

The MSDN article on LNK4098 has a very useful table: it tells you which libraries to manually add to the "Ignore specific library" list, depending on which CRT you're using. You need to pick a CRT (Multithreaded or not; static or DLL; debug or release), and then add the ignore libraries based on your choice.

How do I fix lnk2005?

For example, if you have a method in a namespace to convert a C# Guid to C++ GUID being called in multiple places (i.e. perhaps your engine has multiple CLI Wrappers for different GUIs) then you can fix LNK2005 by making the namespace anonymous. See the example fix below for a demonstration.

Where can I find more information about debug mode?

More information can be found in the Debug pane of the Output Window (Debug->Windows->Output). The weird thing is, building in Debug mode works fine, and building and deploying to the Microsoft Store is also without problems. I've cleaned the obj and bin folders, updated Visual Studio and all used nuget packages, reinstalled Visual Studio...

How do I Turn Off default libraries in Visual Studio?

In the IDE, add references to your project to specify the libraries to use, and then open the Property Pages dialog for your project, and in the Linker, Input property page, set either Ignore All Default Libraries, or Ignore Specific Default Libraries properties to disable the default libraries.


1 Answers

For some reason, you're linking against both LIBCMT and LIBCMTD (the debug version). (From reading the end of each error line: already defined in libcmtd.lib(invarg.obj))

You're fixing the right thing by saying /NODEFAULTLIB:LIBCMT. Does the debug/release flag on the third-party library that you're linking against match the debug/release mode on your app build? I would guess that the third-party code is pulling in a redundant library somehow.

like image 79
sblom Avatar answered Oct 31 '22 18:10

sblom