Compilation Errors in Visual Studio 2012 - c++

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.

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?

LNK2019 unresolved external symbol after moving functions to another header file

I had two array templates in one of my header files, everything worked well. Then I thought, I'd better get some more pedantism into my code, so I moved them all into another header file, mostly just for them (and for another function, that wants to use one of them). And then I got LNK2019 error every time I used functions from these templates in other header files.
Since everything was good before I pasted my code elsewhere, I assume the code is okay, it's just my lack of understanding. Basically, the question is: why do I get a linker error when I moved my function to another header file?
Here is an example of one of my errors:
Error LNK2019 unresolved external symbol "public: char __thiscall C2DArray::Get(int,int)" (?Get#?$C2DArray#D##QAEDHH#Z) referenced in function "public: char __thiscall SGame::GetRecordOutput(int,int)" (?GetRecordOutput#SGame##QAEDHH#Z) Mastermind C:\Users\Master\Documents\Visual Studio 2015\Projects\Mastermind\Mastermind\Menu.obj 1
I'm using Visual Studio 2015 if it matters.
Thank you for the reply. As it seems, I have resolved my problem. The cause was the class templats, which were not specialized (and had to be). All I had to do is include what types of these templates I wanted. Sorry for posting before doing more extensive research.

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

Microsoft Overloading the << Operator Sample throws link errors

I'm trying to compile the Microsoft Sample "Overloading the << Operator for Your Own Classes" but get the following link error:
error LNK1169: one or more multiply defined symbols found
error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Date const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABVDate###Z) already defined in Date.obj
Anybody any idea why this is not compiling?
If you have the definition of the operator inside a header file, you have to declare it inline, otherwise it will be defined in all translation units that include that header.
But it's probably better to move it to a implementation file, unless you have strong reasons for having it in a header.
For me this error was was related to multiple definitions and i solved it following a recommendation from Microsoft. In project properties=>Linker=>Command Line=> Additional Options text box add a command "/FORCE:MULTIPLE". This solved my problem. (https://msdn.microsoft.com/en-us/library/70abkas3.aspx)

how to ignore LNK2005 & LNK1169?

So I have a Visual Studio 2010 project that uses external libraries and in order to get it compile without LNK2005 I had to juggle arround with the order of the libraries in the linker settings.
I got it to compile fine in release mode, but for whatever reasons I don't manage to get it to compile without LNK errors in debug.
Is there no way to generally ignore LNK2005 and tell the linker to simply use whatever he encounters first?
Thanks!
//edit: here are some of the errors output of the PARTICULAR problem. however I already tried to solve that in different ways with each solution giving me different linker problems. hence i'm looking for general solution to ignore LNK2005
Error 7 error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info##AAE#ABV0##Z) already defined in Libcmtd.lib(typinfo.obj)
...\msvcprtd.lib(MSVCP100D.dll)
Error 8 error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info##AAEAAV0#ABV0##Z) already defined in Libcmtd.lib(typinfo.obj)
...\msvcprtd.lib(MSVCP100D.dll)
Error 9 error LNK2005: _exit already defined in Libcmtd.lib(crt0dat.obj)
...\msvcprtd.lib(MSVCP100D.dll)
Error 10 error LNK2005: __invalid_parameter already defined in Libcmtd.lib(invarg.obj)
...\msvcprtd.lib(MSVCP100D.dll)
...
Error 37 error LNK1169: one or more multiply defined symbols found
You may try the linker-option /FORCE (Force File Output in the Linker General tab of the Project Properties). This will force the linker to create a exe/dll even when such errors occur. But its left to you to find out if this exe does work at all or even correctly. After all i would not recommend this strategy.
Linker errors can sometimes be tedious to solve, but usually it has to be done only after migrating or setting up a project. This may take quite a while - it sometimes took me more then a day, but it should be done properly.
You absolutely must not ignore linker errors, ever! A linker is telling you that it's confused about a symbol that's defined in multiple places - where should it take the definition from? Do you really want it to be arbitrary? What about when you change your code and the linker randomly decides to take the other definition which might suddenly break your code?
Instead of fighting the tool, correct your code so that it compiles and links without errors. This MSDN article has some information on fixing it, along with links for more information.