Linker Error "unresolved external symbol" when using constructor of other class - c++

I've been struggling to find why my linker gets an unresolved external symbol error. The error looks like this:
Error
LNK2019
unresolved external symbol "public: __thiscall Shader::Shader(char const *)" (??0Shader##QAE#PBD#Z) referenced in function "public: __thiscall GridWorldGPGPU::GridWorldGPGPU(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int)" (??0GridWorldGPGPU##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##I#Z)
DV2556_Project
grid_world_GPGPU.obj
1
As far as I understand it it has something to do with that my linker finds the declaration of the Shader::Shader(char const *)-function but cannot find the definition. I have been staring at this for hours and cannot figure out why the linker becomes sad.
grid_world_GPGPU.h:
#ifndef GRID_WORLD_GPGPU_H
#define GRID_WORLD_GPGPU_H
#include "grid_world.h"
#include <../swift_open_gl.h>
class GridWorldGPGPU : public GridWorld {
private:
Shader* compute_shader_ = nullptr;
public:
GridWorldGPGPU(std::string in_path_shader, unsigned int in_side = 1);
};
#endif // !GRID_WORLD_GPGPU_H
grid_world_GPGPU.cpp:
GridWorldGPGPU::GridWorldGPGPU(std::string in_path_shader, unsigned int in_side)
: GridWorld(in_side) {
this->compute_shader_ = new Shader(in_path_shader.c_str());
}
The Shader-class is defined in the swift_open_gl.h file:
#ifndef SWIFT_OPEN_GL_H
#define SWIFT_OPEN_GL_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class Shader {
public:
Shader(const char* cs_path);
};
#endif // !SWIFT_OPEN_GL_H
And swift_open_gl.cpp has this:
#include "..\swift_open_gl.h"
inline Shader::Shader(const char * cs_path) {
//Do stuff
}
I've tried with and without the inline (Visual Studio added it when I tried moving the function definition between the .h-file and a .cpp-file, so I decided to try it) and i have verified that the #include <../swift_open_gl.h> doesn't occur anywhere else in the project than the files listed above.
An extra set of eyes to look over this would be appreciated!

Provide default constructor of the class as well.
class Shader {
public:
Shader(){} // default constructor
Shader(const char* cs_path);
};
Dont use inline in Shader::Shader(const char* cs_path){} definition

Related

Linker can't find a namespace's functions

See code below. There's something wrong with it, because the linker is complaining it can't find the Memory's functions, but I can't figure out why.
memory.h
#pragma once
#include "includes.h" //it just includes other strandard headers.
class MemoryUnit
{
public:
MemoryUnit() {}
virtual int getValue() = 0;
virtual int getSize() = 0;
virtual void setValue(int) = 0;
virtual ~MemoryUnit() {};
};
class Byte : public MemoryUnit
{
int value;
public:
static int size;
Byte(int byte) :value(byte) {};
int getSize() { return size; }
int getValue() { return value; };
void setValue(int byte) { value = byte; }
~Byte() {};
};
namespace Memory
{
extern int size;
extern MemoryUnit** map;
void checkAddress(int address);
int read(int adress);
MemoryUnit* getOperation(int address);
void write(int adress, MemoryUnit* data);
void writeByte(int adress, int data);
}
memory.cpp
#include "includes.h"
#include "memory.h"
#include "simulator.h" // it contains only externed constants.
namespace Memory
{
int size = 0;
MemoryUnit** map = NULL;
inline MemoryUnit* getOperation(int address)
{
return map[address];
}
inline void checkAddress(int address)
{
if (address < 0 || address >= MAX_MEMORY_SIZE)
throw std::out_of_range("Invalid memory address.");
}
inline int read(int address)
{
checkAddress(address);
return map[address]->getValue();
}
inline void write(int address, MemoryUnit* data)
{
checkAddress(address);
delete map[address];
map[address] = data;
}
inline void writeByte(int address, int data)
{
checkAddress(address);
map[address]->setValue(data);
}
}
Everywhere the class/namespace memory.h declares is includes memory.h. Is here anything wrong in the code below?
Edit:
I'm using Visual Studio 2015.
Errors I got when building the project:
LNK1120 5 unresolved externals simulator.exe
LNK2019 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" referenced in function "void __cdecl ALU::setFlags(int)" alu.obj
LNK2001 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" cu.obj
LNK2019 unresolved external symbol "class MemoryUnit * __cdecl Memory::getOperation(int)" referenced in function "void __cdecl CU::run(void)" cu.obj
LNK2001 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" helpers.obj
LNK2019 unresolved external symbol "void __cdecl Memory::write(int,class MemoryUnit *)" referenced in function "void __cdecl readProgramCommands(void)" helpers.obj
LNK2001 unresolved external symbol "public: virtual int __thiscall MemoryPointer::getValue(void)" helpers.obj
LNK2001 unresolved external symbol "public: virtual int __thiscall IndirectMemoryPointer::getAddress(void)" helpers.obj
LNK2001 unresolved external symbol "void __cdecl Memory::writeByte(int,int)" main.obj
alu.h and alu.cpp for the first error:
//alu.h
#pragma once
#include "includes.h"
#include "operation.h"
namespace ALU
{
int operation(Operation* op);
void setFlags(int result);
}
//alu.cpp
#include "includes.h"
#include "simulator.h"
#include "alu.h"
#include "memory.h"
#include "operation.h"
namespace ALU
{
int operation(Operation* operation)
{
// ...
setFlags(result);
return result;
}
inline void setFlags(int result)
{
Memory::writeByte(FLAG_Z, result == 0);
// ...
}
}
You need to put the inline function definition inside your header file (they both must appear in every translation unit where they are used), you can separate declaration and definition but both must be in the header file. Also they must be declared as inline.
N4140 dcl.fct.spec 7.1.2.4
An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly
the same definition in every case (3.2). [ Note: A call to the inline function may be encountered before its
definition appears in the translation unit. —end note ] If the definition of a function appears in a translation
unit before its first declaration as inline, the program is ill-formed.
When you're using inline functions or methods, their definitions should be visible for every source unit that uses them. You defined your inline functions in Memory.cpp, that's why you get 'unresolved' linker error.
To fix your problem you can:
Remove inline modifier and keep functions definitions in Memory.cpp.
Keep inline modifier but move functions definitions to Memory.h.

Loki's SmallObjAllocator of of memory

#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),0){};
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier.Deallocate(myChar, 1);
return 0;
}
The loki library uses essentially generic programming in c++
I have got the code up there using Small object allocator of memory(Loki::SmallObjAllocator)
I m using visual c++ 2010
I get those errors:
> MyLoki.cpp
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void __thiscall Loki::SmallObjAllocator::Deallocate(void *,unsigned int)" (?Deallocate#SmallObjAllocator#Loki##QAEXPAXI#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void * __thiscall Loki::SmallObjAllocator::Allocate(unsigned int,bool)" (?Allocate#SmallObjAllocator#Loki##QAEPAXI_N#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "protected: __thiscall Loki::SmallObjAllocator::SmallObjAllocator(unsigned int,unsigned int,unsigned int)" (??0SmallObjAllocator#Loki##IAE#III#Z) referenced in function "public: __thiscall MySmallObj::MySmallObj(void)" (??0MySmallObj##QAE#XZ)
I have founded an answer to my first question.
#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),1){}; //the chunkSize < maxObjsize
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier->Deallocate(myChar, 1);
return 0;
}
the errors of multiple
unresolved external symbol
are because of SmallObj.cpp , of Loki that was not included to the project
for example of Loki::SmallObjAllocator, Loki::SmallObject here

Constant reference parameter causing unresolved external symbol

Below is a simplified version of some code I wrote. this code works fine so far
class.h
namespace myNamespace
{
class myClass
{
public:
myClass(unsigned width, unsigned height);
myClass(OtherClass& other, unsigned width, unsigned height);
~myClass(){};
private:
unsigned width;
unsigned height;
};
}
class.cpp
#include "class.h"
namespace myNamespace
{
myClass::myClass(unsigned width, unsigned height)
{
//code
}
myClass::myClass(OtherClass& other, unsigned width, unsigned height) : myClass(width, height)
{
//code
}
}
(OtherClass is defined elsewhere inside the myNamespace and is included)
the constructor that uses OtherClass doesn't alter other so making it const would be appropriate.
But when I change the constructor in both the .cpp and .h to use const OtherClass& it gives me the error:
error LNK2019: unresolved external symbol "public: __thiscall
myNamespace::myClass::myClass(class myNamespace::OtherClass &,unsigned
int,unsigned int)"
(??0CarbonMatrix#molecule##QAE#AAVCarbonString#1#II#Z) referenced in
function _wmain path\main.obj
As far as I know const shouldn't cause this error as long you use them both at the declaration and at the definition.
So my question: What goes wrong and how to fix it?
That error indicates, that you have actually not changed the declaration in the header file or did not recompile all source files after changing it.
Double check that you indeed changed it to const OtherClass& in the header file. Then re-compile the whole project, i.e. not only class.cpp, but also main.cpp.

std::unordered_map and linker errors

I'm have some problems with compiling my new project.
Compiler outputs:
1>------ Build started: Project: IRPCore, Configuration: Debug Win32 ------
1> core.cpp
1> Creating library D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.lib and object D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.exp
1>core.obj : error LNK2001: unresolved external symbol "public: static class std::tr1::unordered_map<int,struct item,class std::hash<int>,struct std::equal_to<int>,class std::allocator<struct std::pair<int const ,struct item> > > core::itemCache" (?itemCache#core##2V?$unordered_map#HUitem##V?$hash#H#std##U?$equal_to#H#3#V?$allocator#U?$pair#$$CBHUitem###std###3##tr1#std##A)
1>main.obj : error LNK2001: unresolved external symbol "public: static class std::tr1::unordered_map<int,struct item,class std::hash<int>,struct std::equal_to<int>,class std::allocator<struct std::pair<int const ,struct item> > > core::itemCache" (?itemCache#core##2V?$unordered_map#HUitem##V?$hash#H#std##U?$equal_to#H#3#V?$allocator#U?$pair#$$CBHUitem###std###3##tr1#std##A)
1>D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.dll : fatal error LNK1120: 1 unresolved externals
Here comes core.cpp
#include <iostream>
#include <unordered_map>
#include "core.h"
core::core(void)
{
}
core::~core(void)
{
}
void core::Item_Insert(int uid, item Item)
{
core::itemCache.insert(std::make_pair<int,item>(uid, Item));
return;
}
std::string core::convertNativeStringToString(AMX *amx, cell input)
{
char *string = NULL;
amx_StrParam(amx, input, string);
return string ? string : "";
}
Should I link some more libs or so?
Thanks in advance.
Edit: Here comes core definition from core.h
class core
{
public:
static std::unordered_map<int, item> itemCache;
core(void);
~core(void);
static void Item_Insert(int uid, item Item);
static std::string convertNativeStringToString(AMX *amx, cell input);
private:
};
Add a definition of core::itemCache to core.cpp. In general, static data members of classes are declared in the class definition and defined in a source file.
As #Pete Becker is saying, put this at the top of the cpp:
std::unordered_map<int, item> core::itemCache;
in addition to the
static std::unordered_map<int, item> itemCache;
already in the .h.

Link error using std::vector

I'm having a problem with a vector declaration.
Here's the code:
.h
#ifndef ANIMATEDSPRITE_H_
#define ANIMATEDSPRITE_H_
#include "Sprite.h"
#include <vector>
//using namespace std;
class AnimatedSprite //abstract class to point sprites
{
public:
AnimatedSprite();
~AnimatedSprite();
//gets and sets
Sprite GetMySprite(int _index);
void SetSpriteToList(Sprite _sprite);
int GetState() const;
void SetState(int _state);
//other
private:
std::vector<Sprite> spriteList;
int state; //estado que esse sprite representa (parado esquerda, andando direita, etc)
};
#endif
.cpp
#include "AnimatedSprite.h"
AnimatedSprite::AnimatedSprite()
{
spriteList.clear();
state = NULL;
}
AnimatedSprite::~AnimatedSprite()
{
}
Sprite AnimatedSprite::GetMySprite(int _index)
{
return spriteList[_index];
}
void AnimatedSprite::SetSpriteToList( Sprite _sprite )
{
//Sprite* temp = new Sprite(1,2);
spriteList.push_back(_sprite);
}
int AnimatedSprite::GetState() const
{
return state;
}
void AnimatedSprite::SetState( int _state )
{
state = _state;
}
But I'm getting 2 errors:
Error 1 error LNK2019: unresolved external symbol imp_CrtDbgReportW referenced in function "public: class Sprite & __thiscall std::vector >::operator[](unsigned int)" (??A?$vector#VSprite##V?$allocator#VSprite###std###std##QAEAAVSprite##I#Z) AnimatedSprite.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\DevProjects\SDLSkeleton\Debug\SDLSkeleton.exe
I've found a solution removing the _DEBUG from the Preprocessor Definitions, but it seems kinda wrong to do that.
Is it the right solution? What's the consequence of removing it?
In the book and documentations I've checked it should be just a common variable declaration, but this errors showed up.
Thanks.
This is because your build is inconsistent: you define _DEBUG macro, but link with release CRT version (/MD). So either remove _DEBUG, or select /MDd option.