C++ LNK2005 In Inline File - c++11

As the title suggests, I have an linker error relating to my inline file. It is complaining that my dungeon_layout header that includes my dungeon_room header(which includes the inl file) defines something twice. All my headers have guards, I only include .hpp files, and yet it still gets angry when I try and overload the assignment operator. The code which it complains is such:
Where I need assignment 1:
dungeon_room dungeon_layout::getCurrentRoom()
{
...
dungeon_room temp;
for (row = levelLayout.begin(); row != levelLayout.end(); row++)
{
for (col = row->begin(); col != row->end(); col++)
{
if (col->active)
{
...
temp = *col;
...
}
}
...
}
return temp;
}
Where I need assignment 2:
dungeon_room::dungeon_room()
{
...
roomMap = tim::tileMap(20, 20, 50);
...
}
Assignment Operator 1:
tileMap tim::tileMap::operator=(const tileMap& other)
{
// nothing...
}
Assignment Operator 2:
void dungeon_room::operator=(const dungeon_room& other)
{
// nothing...
}
Header for the Tile Manager (inl file definitions for functions)
#ifndef DUNGEON_ROOM_HPP
#define DUNGEON_ROOM_HPP
...
#include "gridManager\tileManager.hpp"
...
#endif
Header for the level layout
#ifndef DUNGEON_LAYOUT_HPP
#define DUNGEON_LAYOUT_HPP
...
#include "dungeon_room.hpp"
...
#endif
Error:
1>dungeon_room.obj : error LNK2005: "private: void __cdecl
tim::tileMap::addSprite(class sf::RectangleShape,unsigned __int64)" (?addSprite#tileMap#tim##AEAAXVRectangleShape#sf##_K#Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "private: void __cdecl tim::tileMap::addSprite(class sf::Sprite,unsigned __int64)" (?addSprite#tileMap#tim##AEAAXVSprite#sf##_K#Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::modifyTile(int,int,int)" (?modifyTile#tileMap#tim##QEAAXHHH#Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::addCollisionValue(int)" (?addCollisionValue#tileMap#tim##QEAAXH#Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow &,class sf::View &)" (?drawTiles#tileMap#tim##QEAAXAEAVRenderWindow#sf##AEAVView#4##Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow *,class sf::View &)" (?drawTiles#tileMap#tim##QEAAXPEAVRenderWindow#sf##AEAVView#4##Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow &)" (?drawTiles#tileMap#tim##QEAAXAEAVRenderWindow#sf###Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::drawTiles(class sf::RenderWindow *)" (?drawTiles#tileMap#tim##QEAAXPEAVRenderWindow#sf###Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: void __cdecl tim::tileMap::operator=(class tim::tileMap const &)" (??4tileMap#tim##QEAAXAEBV01##Z) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: __cdecl tim::tileMap::tileMap(void)" (??0tileMap#tim##QEAA#XZ) already defined in dungeon_layout.obj
1>dungeon_room.obj : error LNK2005: "public: __cdecl tim::tileMap::tileMap(float,float,float)" (??0tileMap#tim##QEAA#MMM#Z) already defined in dungeon_layout.obj
1>F:\Desktop Files\C++\Games\Dungeon_Seeker\x64\Debug\Dungeon_Seeker.exe : fatal error LNK1169: one or more multiply defined symbols found
I believe that this should be the relevant code. I've been stuck, and debugging for a while now, and it's been annoying. Please help, thanks!

The reason you are getting these errors is because the function is being compiled for each .cpp file it has been compiled in, which is then being exported multiple times, once for each .cpp file that includes your inline file.
The .inl file that contains the definition is pasted into each of the .cpp files that includes it. Meaning, when the program is linked together, there is a definition of the function, that was supposed to only be defined once, in several files. This breaks the One Definition Rule and is the reason the linker is giving you that error.
To fix it, you have three options. First, you can mark the functions as inline, which does not necessarily make the compiler inline the function, but allows the function to be defined multiple times. Second, you can move the definition of the function inside the definition of the class, which implicitly marks the functions as inline. Or you can define the function in a .cpp file.
With you code, if you still want to have the functions declared in an .inl file you can put inline in front of the definitions of the function in the error messages. In this case those functions are
tim::tileMap::addSprite
tim::tileMap::modifyTile
tim::tileMap::addCollisionValue
tim::tileMap::drawTiles //All 4 overloads
tim::tileMap::operator=
tim::tileMap::tileMap //Both constructor overloads

Related

Why doesn't make_unique work with a class which has an inline constructor? [duplicate]

This question already has answers here:
Moving inline methods from a header file to a .cpp files
(6 answers)
Closed 4 years ago.
I have a well defined class like this,
class A
{
public:
A();
A(int a);
A(const A& o);
A(A&& o);
~A();
A& operator=(const A& o);
A& operator=(A&& o);
private:
int a = 0;
};
At first, all the methods are in the .h file. Then I used the VS's refactoring function and moved all these methods to the .cpp file (It saved me a lot of labor.)
All the auto moved methods have a inline key word before them,
inline A::A()
{
cout << "A constructor\n";
}
I know inline keyword, so I think it's OK.
Then I used this class in the third source file (.cpp file).
void test()
{
std::unique_ptr< A> a = std::make_unique<A>();
}
When I compiled the project, It gave me an error LNK2019 happened in make_unique.
Then I deleted all the inline keyword, it worked.
I don't know why make_unique doesn't work when the class's consturctor is inline.
I have thought it's an unique_ptr problem. I just tested,it's a moving inline function to .cpp file problem.
With an inline constructor in .cpp file. Just creating a class A instance would fail.
A a;
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall
__AWellDefinedClass::A::A(void)" (??0A#__AWellDefinedClass##QAE#XZ) referenced in function "void __cdecl __pointerTest::test(void)"
(?test#__pointerTest##YAXXZ) ForTest D:\test\ForTest\ForTest\pointerTest.obj 1

Getting linking error 2019 although class is not derived from CString class in VS2010

i am getting the following error
error LNK2019: unresolved external symbol "public: class
ATL::CStringT > > __thiscall
CFILicenseSettings::GetCodeStartup1(void)"
(?GetCodeStartup1#CFILicenseSettings##QAE?AV?$CStringT#DV?$StrTraitMFC_DLL#DV?$ChTraitsCRT#D#ATL#####ATL##XZ)
referenced in function
__catch$?ValidateLicense#CFIServerDlg##AAE_NXZ$0 E:\TIMSWebServerManager_lic\TIMSWebServerManager\TIMSWebServerManager.obj TIMSWebServerManager
My function defination is as follows:
CString CFILicenseSettings::GetCodeStartup1()
{
CString a = "why";
return a;
}
the declaration of the function is as follows:-
CString GetCodeStartup1();
When i searched the web for the above error i found that this error is because my class is derived from CString class,but this is not the case.....
I get this error when i call the function GetCodeStartup1();
if i comment the calling of function GetCodeStartup1(); my program compile successfully
How can i remove the error?
Thanks in advance

How to solve a linker error with ExitWindowsEx in Windows.h

I am recieving the following errors when I attempt to build and run my code in Visual Studio Express 2010:
1> Generating Code...
1>Purple Health.obj : error LNK2028: unresolved token (0A000035) "extern "C" int __stdcall ExitWindowsEx(unsigned int,unsigned long)" (?ExitWindowsEx##$$J18YGHIK#Z) referenced in function "private: void __clrcall PurpleHealth::FormOutOfTime::btnLogout_Click(class System::Object ^,class System::EventArgs ^)" (?btnLogout_Click#FormOutOfTime#PurpleHealth##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>FormLauncher.obj : error LNK2028: unresolved token (0A000031) "extern "C" int __stdcall ExitWindowsEx(unsigned int,unsigned long)" (?ExitWindowsEx##$$J18YGHIK#Z) referenced in function "private: void __clrcall PurpleHealth::FormOutOfTime::btnLogout_Click(class System::Object ^,class System::EventArgs ^)" (?btnLogout_Click#FormOutOfTime#PurpleHealth##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>FormLauncher.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ExitWindowsEx(unsigned int,unsigned long)" (?ExitWindowsEx##$$J18YGHIK#Z) referenced in function "private: void __clrcall PurpleHealth::FormOutOfTime::btnLogout_Click(class System::Object ^,class System::EventArgs ^)" (?btnLogout_Click#FormOutOfTime#PurpleHealth##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z)
1>Purple Health.obj : error LNK2001: unresolved external symbol "extern "C" int __stdcall ExitWindowsEx(unsigned int,unsigned long)" (?ExitWindowsEx##$$J18YGHIK#Z)
1>C:\Users\Graham\documents\visual studio 2010\Projects\Purple Health\Debug\Purple Health.exe : fatal error LNK1120: 3 unresolved externals
My code looks like this (With the unimportant bits edited out):
#include "FormParentalOverride.h"
#include "Windows.h"
#include <iostream>
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#pragma once
namespace PurpleHealth {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Text;
public ref class FormOutOfTime : public System::Windows::Forms::Form
{
private: System::Void btnLogout_Click(System::Object^ sender, System::EventArgs^ e) {
ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MAJOR_APPLICATION);
//ExitWindows(0, 0);
}
};
}
Any suggestions are greatly appreciated!
Add User32.dll to your link stage.
Note: Name is misleading inasmuch that the 64 and 32 bit versions are both called user32.
use #include< windows.h> instead of #include "Windows.h"

Opencv2.1 application build in VS2010

I have an openCv application that builds and runs fine when using the Mutithreaded Debug Dll option for code generation property. However, I would like to run the application on any computer however, the build fails in Multithreaded Debug mode giving LNK 2005 errors, some examples are
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall
std::_Lockit::~_Lockit(void)" (??1_Lockit#std##QAE#XZ) already defined
in libcpmtd.lib(xlock.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall
std::_Lockit::_Lockit(int)" (??0_Lockit#std##QAE#H#Z) already defined
in libcpmtd.lib(xlock.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "void __cdecl
std::_Debug_message(wchar_t const *,wchar_t const *,unsigned int)"
(?_Debug_message#std##YAXPB_W0I#Z) already defined in
libcpmtd.lib(stdthrow.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall
std::_Container_base12::~_Container_base12(void)"
(??1_Container_base12#std##QAE#XZ) already defined in opencv2.obj
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: "public: __thiscall
std::exception::exception(class std::exception const &)"
(??0exception#std##QAE#ABV01##Z) already defined in
LIBCMTD.lib(stdexcpt.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: void
__thiscall std::_Container_base12::_Orphan_all(void)" (?_Orphan_all#_Container_base12#std##QAEXXZ) already defined in
opencv2.obj
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: __invalid_parameter
already defined in LIBCMTD.lib(invarg.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: __CrtDbgReportW already
defined in LIBCMTD.lib(dbgrptw.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: "public: __thiscall
std::exception::exception(char const * const &)"
(??0exception#std##QAE#ABQBD#Z) already defined in
LIBCMTD.lib(stdexcpt.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: "public: virtual
__thiscall std::exception::~exception(void)" (??1exception#std##UAE#XZ) already defined in
LIBCMTD.lib(stdexcpt.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _fclose already defined
in LIBCMTD.lib(fclose.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _fprintf already defined
in LIBCMTD.lib(fprintf.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _fopen already defined
in LIBCMTD.lib(fopen.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _memmove already defined
in LIBCMTD.lib(memmove.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _free already defined in
LIBCMTD.lib(dbgfree.obj)
Any suggestions would be appreciated. I am really stuck here, there is no problem with smaller programs
Thanks
P.S I also use functions from cvBlobslib and code from LIBSVM but it should not be the problem
If you change to use /MT and /MTd, then all your dependent libraries also need to have been compiled with these settings.
If this is not the case, then these dependencies require linking to the dynamic CRT and your project is trying to link to the static CRT, causing the "already defined" errors you're seeing.

Using PCL with Visual Studio 2010

I am quite new at working with libraries, and I have some problems.
I have installed PCL and all dependencies on my computer (Windows 7, 32 bit) and I can build and use simple examples like that "Simple Cloud Visualization" example. But if I want to use more complete sample, Visual Studio 2010 reports linking errors.
If I understand correctly, I did not 'include' all necessary "Additional Dependencies" in "Linker/Input". I tried to include all .lib files from lib directory, but the error report is still the same.
Does anyone know, which .lib should be added as "Additional Dependencies" to make more complete sample work? Or is problem somewhere else?
error report:
1>main.obj : error LNK2019: unresolved external symbol "public: class vtkProperty * __thiscall vtkActor::GetProperty(void)" (?GetProperty#vtkActor##QAEPAVvtkProperty##XZ) referenced in function "public: bool __thiscall pcl::visualization::PCLVisualizer::addSphere(struct pcl::PointXYZ const &,double,class std::basic_string,class std::allocator > const &,int)" (??$addSphere#UPointXYZ#pcl###PCLVisualizer#visualization#pcl##QAE_NABUPointXYZ#2#NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z)
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl pcl::console::print(enum pcl::console::VERBOSITY_LEVEL,char const *,...)" (?print#console#pcl##YAXW4VERBOSITY_LEVEL#12#PBDZZ) referenced in function "public: bool __thiscall pcl::visualization::PCLVisualizer::addSphere(struct pcl::PointXYZ const &,double,class std::basic_string,class std::allocator > const &,int)" (??$addSphere#UPointXYZ#pcl###PCLVisualizer#visualization#pcl##QAE_NABUPointXYZ#2#NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall vtkSmartPointerBase::~vtkSmartPointerBase(void)" (??1vtkSmartPointerBase##QAE#XZ) referenced in function "public: __thiscall vtkSmartPointer::~vtkSmartPointer(void)" (??1?$vtkSmartPointer#VvtkLODActor####QAE#XZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall vtkSmartPointerBase::vtkSmartPointerBase(void)" (??0vtkSmartPointerBase##QAE#XZ) referenced in function "public: __thiscall vtkSmartPointer::vtkSmartPointer(void)" (??0?$vtkSmartPointer#VvtkLODActor####QAE#XZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall vtkSmartPointerBase::vtkSmartPointerBase(class vtkSmartPointerBase const &)" (??0vtkSmartPointerBase##QAE#ABV0##Z) referenced in function "public: __thiscall vtkSmartPointer::vtkSmartPointer(class vtkSmartPointer const &)" (??0?$vtkSmartPointer#VvtkProp####QAE#ABV0##Z)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall vtkSmartPointerBase::vtkSmartPointerBase(class vtkObjectBase *)" (??0vtkSmartPointerBase##QAE#PAVvtkObjectBase###Z) referenced in function "public: __thiscall vtkSmartPointer::vtkSmartPointer(class vtkSmartPointer const &)" (??$?0VvtkLODActor###?$vtkSmartPointer#VvtkProp####QAE#ABV?$vtkSmartPointer#VvtkLODActor#####Z)
1>main.obj : error LNK2019: unresolved external symbol "public: class vtkSmartPointerBase & __thiscall vtkSmartPointerBase::operator=(class vtkObjectBase *)" (??4vtkSmartPointerBase##QAEAAV0#PAVvtkObjectBase###Z) referenced in function "public: class vtkSmartPointer & __thiscall vtkSmartPointer::operator=(class vtkSmartPointer const &)" (??$?4VvtkLODActor###?$vtkSmartPointer#VvtkProp####QAEAAV0#ABV?$vtkSmartPointer#VvtkLODActor#####Z)
Probably you installed the wrong PCL version (64 bit / 32 bit). Check your VS version and install PCL accordingly. Note that even if you are using a 64 bit system, your compiler may be working in 32 bit.

Resources