I have inherited a C++ solution with 3 projects, one compiling to a .DLL, the other two to .EXEs . The DLL builds on its own fine, but the other two, when built, produce around 65 LNK2005 errors, the majority of which are referencing the same .obj file, as shown in the log exert below:
Linking...
Function.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specification
Function.obj : error LNK2005: _ReadLocalRegister already defined in Function.obj
Function.obj : error LNK2005: _getSource already defined in Function.obj
Function.obj : error LNK2005: _SendLogEvent already defined in Function.obj
Function.obj : error LNK2005: _DebugMsg already defined in Function.obj
Function.obj : error LNK2005: _MyInformationMsg already defined in Function.obj
MyNTService.obj : error LNK2005: "public: __thiscall CMyNTService::CMyNTService(void)" (??0CMyNTService##QAE#XZ) already defined in MyNTService.obj
MyNTService.obj : error LNK2005: "public: virtual void __thiscall CMyNTService::OnStop(void)" (?OnStop#CMyNTService##UAEXXZ) already defined in MyNTService.obj
MyNTService.obj : error LNK2005: "public: void __thiscall CMyNTService::SaveStatus(void)" (?SaveStatus#CMyNTService##QAEXXZ) already defined in MyNTService.obj
....and so it goes on!
I am a C# coder, only basic C++ knowledge, so I am lost with this. The solution is an 15 year old C solution I am attempting to rebuild as a C++ solution in VS2008. I have managed to build it once, nothing has changed, but perhaps some config settings have changed since then.
Does anyone have ideas where I could start to look...?
Many thanks!
It sounds like you're missing inclusion guards on the header file for that object.
Add:
#ifndef SomeUniqueName
#define SomeUniqueName
//Code goes here.
#endif
Wrapping the code in the header file. The compiler will go through that header file many times when processing your code as it's included in many places (most likely). The inclusion guards stop redefinition of things that have already been defined on previous passes.
PS: It also might help to do a "make clean". Makefiles can be finnicky especially if not made 100% right and sometimes when dependencies are off you need to clean before rebuilding.
in msvc110 (2012), I encountered the same error and the only workaround i found was to create a class and a separate DLL for the recurrent functions I used in the different files.
Here's the link to do so.
Cheers
Related
I am working on a dll project, and it had been working well until I added a new .h, which includes a global function and a struct. Then the compiling (or link) failed.
Here is the error message:
1>RobotReality.obj : error LNK2005: "double __cdecl GetNumber(void)" (?GetNumber##YANXZ) [[已经在 dllmain.obj 中定义 translate: this has been defined in dllmain.obj]]
1>stdafx.obj : error LNK2005: "double __cdecl GetNumber(void)" (?GetNumber##YANXZ) [[已经在 dllmain.obj 中定义 translate: this has been defined in dllmain.obj]]
I have added "#pragma once" but the problem still exits.
How to solve this problem?
Thanks!
Declare them 'static' and let the compiler do the work.
Having followed the instructions given here http://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm#apiguide/c/using_the_c__testsocketclient_sample_program.htm
under Visual Studio Professional 2010, the c++ project always gives the following output:
1> Generating Code...
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> Creating library C:\Syncing\VCPlusPlus\IBTestSocketClientVS2010\Debug\SampleSocketClient.lib and object C:\Syncing\VCPlusPlus\IBTestSocketClientVS2010\Debug\SampleSocketClient.exp
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>C:\Syncing\VCPlusPlus\IBTestSocketClientVS2010\Debug\SampleSocketClient.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:01:30.63
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If anyone has had experience of this or understands how to fix this bug, it would be much appreciated!
I guess I found the answer for you. I followed the indication of the error LNK2005 description and it indicates that there is some double linking thing happening behind the scenes.
The only thing you need to do is change the properties of your project Configuration:
Properties->Linker->Input->Ignore Specific Default Libraries
Here, you should set up the library you wanna ignore which is libcmt.lib. After setting up, you should see the value of Ignore Specific Default Libraries is:
libcmt.lib;%(IgnoreSpecificDefaultLibraries)
Then build, binggo~~~~ Please try it out.
Looks like you need to fix your lib references. By the looks of below error, your project is referencing both the release and debug versions, hence the linker is not sure where to look for specific symbols as it is defined in multiple places (both debug and release):
BlockquoteLIBCMT.lib(invarg.obj) : error LNK2005: ___pInvalidArgHandler already defined in libcmtd.lib(invarg.obj)
In case you are not referencing any libs and the IB dll is doing it behind the scenes, you may want to explore the NODEFAULTLIB option through your VS.
The problem is that the PosixSocketClient code assumes that when compiled with a Microsoft compiler, Microsoft Foundation Library (MFC) is going to be used. Maybe it used to work like that in the past but I never got it working with VS2010. But I did get it working by removing dependency on/linking with MFC.
Unfortunately, code in Shared subfolder automatically assumes MFC if _MSC_VER is defined.
(_MSC_VER gives the version of Microsoft C++ compiler).
It cannot be simply undef-ined because some standard Windows headers use it.
This is clearly a bug as the presence of MFC is signalled by _MFC_VER, not _MSC_VER.
Therefore I had to change
#ifdef _MSC_VER
everywhere in EClientSocketBaseImpl.h into
#ifdef _MFC_VER
Also, in file _StdAfx.h, #include <afxwin.h> should be wrapped additionally in a conditional compilation directive:
#ifdef _MFC_VER
#include <afxwin.h>
#endif
It has been about a year since I wrote any .dll plugins that use LibCurl, but I decided to start on another one tonight. Unfortunately, I have been combating the same linker errors for almost 4 hours now, and I am completely lost.
I am statically building LibCurl, and it appears to be fine for the most part, except I am getting the following linker errors.
1>MSVCRT.lib(MSVCR90.dll) : error LNK2005: _calloc already defined in memoverride.obj
1>MSVCRT.lib(MSVCR90.dll) : error LNK2005: _realloc already defined in memoverride.obj
1>MSVCRT.lib(MSVCR90.dll) : error LNK2005: _free already defined in memoverride.obj
1>MSVCRT.lib(MSVCR90.dll) : error LNK2005: _malloc already defined in memoverride.obj
1>MSVCRT.lib(MSVCR90.dll) : error LNK2005: __strdup already defined in memoverride.obj
1>MSVCRT.lib(MSVCR90.dll) : error LNK2005: __malloc_crt already defined in memoverride.obj
I have tried...
1) Reorganizing my included libs due to MSDN saying LNK2005 was caused by this.
2) Rebuilding the LibCurl lib through MSVC Application, then through the Command-Line tool.
3) Excluding the libs that are causing issues.
and a bunch of other things that I cannot even remember now. Can anyone help me out with some information as to how I can solve these?
Problem was my .dll was /MT while the libcurl was /MD. Also, I had initially blocked "libcmt", so when I was switching libcurl to /MT, it was spewing errors.
I have a Visual Studio 6 workspace I'm trying to convert to a Visual Studio 2008 solution. The output of said solution is a .dll. It has to be a .dll and it needs to statically link MFC as I can't redistribute MFC to existing customers.
The solution consists of three projects, say A, B, C. C is the Active Project, outputs the .dll and depends on B. B outputs a .lib and depends on A. A outputs a .lib.
In the General configuration properties I have A and B set to Static Library (.lib) and C set to Dynamic Library (.dll). All three projects are set to "Use MFC in a Static Library." Also, all three projects are set to "Multi-threaded" for Runtime Library and none of them have _AFXDLL defined.
Everything builds correctly up until the final linking stage where I see this:
1>nafxcw.lib(wincore.obj) : error LNK2005: _IsPlatformNT already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _InitMultipleMonitorStubs already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xGetSystemMetrics#4 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xMonitorFromPoint#12 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xMonitorFromRect#8 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xMonitorFromWindow#8 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xGetMonitorInfo#8 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xEnumDisplayMonitors#16 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _xEnumDisplayDevices#16 already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnGetSystemMetrics already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnMonitorFromWindow already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnMonitorFromRect already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnMonitorFromPoint already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnGetMonitorInfo already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnEnumDisplayMonitors already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_pfnEnumDisplayDevices already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_fMultiMonInitDone already defined in A.lib(Globals.obj)
1>nafxcw.lib(wincore.obj) : error LNK2005: _g_fMultimonPlatformNT already defined in A.lib(Globals.obj)
1>nafxcw.lib(viewprnt.obj) : error LNK2005: "public: virtual int __thiscall CPrintingDialog::OnInitDialog(void)" (?OnInitDialog#CPrintingDialog##UAEHXZ) already defined in B.lib(ImagePropertiesDlg.obj)
1>nafxcw.lib(viewprnt.obj) : error LNK2005: "public: __thiscall CPrintingDialog::CPrintingDialog(class CWnd *)" (??0CPrintingDialog##QAE#PAVCWnd###Z) already defined in B.lib(ImagePropertiesDlg.obj)
1>nafxcw.lib(viewprnt.obj) : error LNK2005: "public: virtual void __thiscall CPrintingDialog::OnCancel(void)" (?OnCancel#CPrintingDialog##UAEXXZ) already defined in B.lib(ImagePropertiesDlg.obj)
I've Googled the problem to death and seen other people with a similarish issue, but can't seem to solve it. I tried adding nafxcw.lib to the Ignored libraries for C, but that turned this batch of linker errors into 1500+ unresolved symbol errors. I can get it to compile/link if I make it dynamically link MFC, but like I said, I need it to statically link. I feel like I'm missing something rather simple but can't seem to wrap my head around it. Any and all random thoughts and ideas will be greatly appreciated.
As is often the case, the solution turned out to be so mundane and obvious, I'm still kicking myself for banging my head on it for so long.
Basically, project A referenced above was not a project I directly pulled from the old VS6 workspace, but rather a project that had previously been converted by another team for use in another application. I assumed it was functionally equivalent to the original one, but that is very much not the case. These linking errors are actual, valid multiply defined linking errors. When I started digging in, I found that the first batch of errors was from including multimon.h in project A (something I don't even need, just removed it) and the second batch is from a class that is actually defined in both A and B.
Like I said, I'm still groaning over here, but at least I should be able to fix my problem now. Thanks for the help and always remember to NEVER assume ANYTHING.
When you tried ignoring the nafxcw library, did you add it back in to try to force a link order as suggested by this KB article? While your problem isn't an exact fit it may be related. Perhaps things could be resolved by monkeying with the link order.
Here is My Scenario.
I have an Existing Solution in visualstudio which contains 15 projects.
in some project i need to use managed c++ extensions. So first i have a workaround for sample solution contains 4 projects in those i have maintained dependencies b/w them.
and my solution is builded also.
I apply the same procedure to existing solution by adding extra project which contains managed code.so that i enable CLR in calling project(from where i call new added project methods from existing ex:demoproj) as well as project which contains main().and change the runtime from /mtd(existing one contains /mtd) to /mdd.
when i build the demoproj it builds success fully. But when i building project which contains main() it gives lot of errors like
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
LIBCMTD.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(invarg.obj) : error LNK2005: __invalid_parameter already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)
LIBCMTD.lib(fclose.obj) : error LNK2005: _fclose already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(printf.obj) : error LNK2005: _printf already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(mbstowcs.obj) : error LNK2005: _mbstowcs already defined in MSVCRTD.lib(MSVCR80D.dll)
I found this is happend because of using /clr option i think any idea???
But this case is not happend for my test solution.How to resolve this
Any help is greatly appreciated
All the dlls you link to using import libraries must have the same setting as the application for how you link to the runtime library. The errors are showing that you are mixing use of the runtime as a .dll (MSVCRTD) and as a .lib (LIBCMTD).