c++ error lnk2005 when a global function is included - c++

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.

Related

Linker error building Qt 5.6 - multiply defined symbols

Trying to build Qt 5.6 from source on Windows 7 using MSVC13. Compilation goes fine, but I get two linker errors regarding multiply defined symbols:
Qt5SerialBus.lib(qmodbusdevice.obj) : error LNK2005: "class QLoggingCategory const & __cdecl QT_MODBUS(void)" (?QT_MODBUS##YAAEBVQLoggingCategory##XZ) already defined in mainwindow.obj
Qt5SerialBus.lib(qmodbusdevice.obj) : error LNK2005: "class QLoggingCategory const & __cdecl QT_MODBUS_LOW(void)" (?QT_MODBUS_LOW##YAAEBVQLoggingCategory##XZ) already defined in mainwindow.obj
release\adueditor.exe : fatal error LNK1169: one or more multiply defined symbols found
I suspect the problem (and solution) are similar to this one in which the offending functions can just be marked inline, however I can't find the definitions for these two functions anywhere in the modbus code. Does anyone know where "QT_MODBUS" and "QT_MODBUS_LOW" are defined, or is there another workaround to this?

Compilation Errors in Visual Studio 2012

I'm been trying for the last couple of hours to solve an error that I can't get rid of. Here it is the gist: https://gist.github.com/pluralism/11294490
I'm out of ideas because everything looks correct for me. The compilation error I'm getting is the following:
1>MenuCAL.obj : error LNK2005: "void __cdecl printSquareArray(int * *,unsigned int)" (?printSquareArray##YAXPAPAHI#Z) already defined in calproject.obj
1>C:\Users\Andre Pinheiro\Desktop\calproject\Debug\calproject.exe : fatal error LNK1169: one or more multiply defined symbols found
If you need other files please feel free to ask. Thanks in advance!
If you want to define printSquareArray in a header, mark it inline to allow a definition in every translation unit that includes the header.
Alternatively, move the definition into a source file, so it's only defined once.

C++ LNK2005 'already defined' errors - files referencing themselves

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

How can the Interactive Brokers API C++ test socket client be built with VS2010

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

Problem statically linking MFC libraries

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.