This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I have 3 headers
DirectX.h,Draws.h,Memory.h d
Every function has a definition in its corresponding .cpp so that is ruled out except for DirectX.h ofc
I tried a set of solutions to fix it but without success, like not including stdafx.h in all on the Headers.
A little snippet of the three
Memory.h
#pragma once
#include "stdafx.h"
Class Memory
{
foo..
};
extern Memory *gMemory;
Draws.h
#pragma once
#include "stdafx.h"
Class Drawing
{
foo..
};
extern Drawing *Draw;
DirectX.h
#pragma once
#include "stdafx.h"
struct Direct
{
foo
};
extern Direct *DirectX;
Error
1>dllmain.obj : error LNK2001: unresolved external symbol "class Memory * gMemory" (?gMemory##3PAVMemory##A)
1>dllmain.obj : error LNK2001: unresolved external symbol "class Drawing * Draw" (?Draw##3PAVDrawing##A)
1>dllmain.obj : error LNK2001: unresolved external symbol "struct Direct * DirectX" (?DirectX##3PAUDirect##A)
1>Draws.obj : error LNK2001: unresolved external symbol "struct Direct * DirectX" (?DirectX##3PAUDirect##A)
stdafx.h
#include <windows.h>
#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>
#include "Memory.h"
#include "Draws.h"
#include "DirectX.h"
dllmain.cpp
The only parts where I'm using the extern are
gMemory->FindPattern(..);
if (!DirectX->Line)
{
D3DXCreateLine(pDevice, &DirectX->Line);
}
Draw->Circle(foo);
extern keyword means, that object is defined somewhere else (in other compilation unit, in linked static lib), in simple words, it's link to the objects, that are instantiated (created) somewhere else, but can be used in the current compilation unit.
In you dllmain.cpp you should declare variables in global (for example) scope:
// Declaration of pointers.
Drawing* Draw = NULL;
Direct* DirectX = NULL;
Memory* gMemory = NULL;
and in int dllmain() (or whatever main function you have):
// Initialization of pointers with newly created objects:
Draw = new Drawing();
DirectX = new Direct();
gMemory = new Memory();
Related
This is my eventhandler.h
#pragma once
#include <queue>
#include <Windows.h>
class EventHandler
{
public:
EventHandler()
{
}
~EventHandler()
{
}
static std::queue<MSG*> Events;
};
I've searched a lot to try and solve my problem and all the answers say to declare the static variable in a c++ file, which I've done
#include "EventHandler.h"
std::queue<MSG*> EventHandler::Events;
but I still get
Error LNK2001 unresolved external symbol "protected: static struct tagMSG * Entity::msg" (?msg#Entity##1PAUtagMSG##A)
and I can't figure out why. Have I missed something?
You also need to place your static in cpp file:
// EventHandler.cpp
std::queue<MSG*> EventHandler::Events;
This question already has answers here:
Undefined reference to static variable c++
(3 answers)
Closed 5 years ago.
I have class World in world.h:
class World
{
public:
static Ground* ground;
};
and in another class in a function I try to use the static variable ground like that:
#include "Node.h"
#include "World.h"
void Node::Foo()
{
Ground* ground = World::ground;
}
and also in world.cpp i have:
#include "stdafx.h"
#include "World.h"
static Ground* ground = new Ground(10, 10);
But i get the following errors:
LNK2001 unresolved external symbol "public: static class Ground World::ground" (?ground#World##2PAVGround##A)
LNK1120 1 unresolved externals
static Ground* ground = new Ground(10, 10);
You're missing World:: there, so you're defining a completely unrelated variable that just happens to have the same name. You should have this:
Ground* World::ground = new Ground(10, 10);
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall Sounds::soundBox(void)" (?soundBox#Sounds##QAEXXZ) referenced in function _main
For some reason i get this error, and i rly dont know what i did wrong.
Got wimm.lib added playsound works when called from main()
When i try to call it from class in playsound.cpp it calls error...
playsounds.h
#pragma once
#include <Windows.h>
class Sounds
{
public:
Sounds();
~Sounds();
void soundBox();
};
playsound.cpp
#include "playsound.h"
Sounds::Sounds()
{
}
void soundBox()
{
PlaySound(TEXT("fx/boom1.wav"), NULL, SND_FILENAME);
}
Sounds::~Sounds()
{
}
main.cpp
#include <iostream>
#include <conio.h>
#include "playsound.h"
int main()
{
Sounds newsound;
newsound.soundBox();
_getch();
}
You need to change the function definition in playsound.cpp
void soundBox()
To
void Sounds::soundBox()
This is because the function exists within the scope of the Sounds class, so you have to define it as such. Otherwise it would be a free function, and the version of the function in your Sounds class would be undefined (which is what the error is telling you).
I have a C++ aplication with one form (Form1.h) and a plugin.ccp file that is the actual application .The program is a plugin for Mach3 cnc controler which comunicates with the cnc machine by USB.
I want a global variable that can be used in Form1.h and in plugin.ccp.
A tried with a solution i found on this site.
Form1.h :
extern BOOL B1;
Form1.ccp
#include "Form1.h"
BOOL B1 = TRUE ;
plugin.ccp
#include "Form1.h"
And it compiles without errors .
But when a type something like this
Form1.h
B1 = FALSE;
// or
SomeOtherVar = B1;
It gives me
Error 1 error LNK2020: unresolved token (0A00003B) "int mach_plugin::B1" (?B1#mach_plugin##3HA) E:\mach_vmotion\Plugin.obj mach_vmotion
Error 2 error LNK2020: unresolved token (0A00000E) "int mach_plugin::B1" (?B1#mach_plugin##3HA) E:\mach_vmotion\Form1.obj mach_vmotion
Error 3 error LNK2001: unresolved external symbol "int mach_plugin::B1" (?B1#mach_plugin##3HA) E:\mach_vmotion\Form1.obj mach_vmotion
Error 4 error LNK2001: unresolved external symbol "int mach_plugin::B1" (?B1#mach_plugin##3HA) E:\mach_vmotion\Plugin.obj mach_vmotion
Error 5 error LNK1120: 3 unresolved externals E:\mach_vmotion\Debug\mach_vmotion.dll mach_vmotion
The error message indicates you have something like :
namespace mach_plugin {
#include "form1.h"
}
while your B1 is defined at global scope. Make up your mind where it belongs and make the declaration in header match -- and avod including files inside any blocks, namespace, extern "C", whatever.
In general, don't try to assign to variables in header files. Whether you put these assignments before or after the extern BOOL B1 (or deleted that line entirely), it won't work correctly. Instead, assign B1 and SomeOtherVar where they're defined (either via initialization or inside a function).
inside Form1.cpp. Try
#include "Form1.h"
namespace mach_plugin{
BOOL B1 = TRUE ;
}
You need to post more codes so that we may see it clearly.
I have two files that are causing me a lot of grief: camAVTEx.h and camAVTEx.cpp. Here is the general setup for the two files:
//.h////////////////////////////////////////////////
/*
#includes to some other files
*/
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
private:
//some members
public:
//some methods
};
void GlobalShutdownVimbaSystem();
//.cpp/////////////////////////////////////////////
#include "StdAfx.h"
#include "camAVTEx.h"
//some other #includes
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
}
//rest of the class' functions
void GlobalShutdownVimbaSystem()
{
//implememtation
}
Then, in a file in a different directory, I do a #include to the exact location of the .h file and try to use the class:
//otherfile.cpp
#include "..\..\src\HardSupport\Camera.h"
//this is the base camera class (camera_t)
#include "..\..\src\HardControl\camAVTEx.h"
//this is indeed where both the .h and .cpp files are located
void InitCam
{
camera_t* maincam = new camera_avtcam_ex_t();
}
void OnExit()
{
GlobalShutdownVimbaSystem();
}
When I compile, I get the following errors:
8>otherfile.obj : error LNK2001: unresolved external symbol "public: __cdecl
camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ)
8>otherfile.obj : error LNK2001: unresolved external symbol "void __cdecl
GlobalShutdownVimbaSystem(void)" (?GlobalShutdownVimbaSystem##YAXXZ)
8>....\bin\x64\Release\otherfile.exe : fatal error LNK1120: 2 unresolved externals
I cannot for the life of me figure out why it can't find the implementations for these two functions.
So I guess my question is fairly obvious: Why am I getting these errors and what do I need to change to fix them?
Whatever how you look at it, the error you have : unresolved external symbol "public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ) means that the compiler knows the symbol camera_avtcam_ex_t::camera_avtcam_ex_ (that's the class constructor) since he saw its declaration in the camAVTEx.h file but halas, it can't find (= resolve) the implementation of this symbol (in short, the code).
This usually happen because of several possible causes :
you didn't tell the compiler about the code (.cpp) you try to use so he doesn't know it. Try to add the file to your project.
you compile the missing code, but don't link with it. Check if you don't have two separated projects or try to add the lib to your project if it comes from a lib.
in some way, the compiled code does not match its definition (happens when mixing C and C++ or messing with namespaces) Check if you are not declaring contradicting enclosing namespaces.
(maybe other reasons I don't know ?)