Global variable in c++ error LNK2020 - c++

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.

Related

The good ol' LNK2001 [duplicate]

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();

Getting "error LNK2019: unresolved external symbol ... "

I'm trying to get started with C++ but I keep getting this error. I know which parts of my code is generating it, but I think that at least one these parts shouldn't generate them.
I am creating a class called Text that is functioning in a way similar to the std::string class, just to experiment and get a better understanding of value semantics.
Anyhow, these are my files:
Text.h:
#ifndef TEXT
#define TEXT
class Text {
public:
Text(const char *str);
Text(const Text& other);
void operator=(const Text& other);
~Text();
private:
int size;
char* cptr;
};
#endif
Text.cpp:
#include "Text.h"
#include <cstring>
#include <iostream>
using namespace std;
Text::Text(const char* str) {
size = strlen(str) + 1;
cptr = new char[size];
strcpy(cptr, str);
}
Text::Text(const Text& other) {
size = other.size;
cptr = new char[size];
strcpy(cptr, str);
}
void Text::operator=(const Text& other){
delete [] cptr;
size = other.size;
cptr = new char[size];
strcpy(cptr, other.ctpr);
}
Text::~Text() {
delete [] cptr;
}
Main.cpp:
#include <iostream>
#include "Text.h"
using namespace std;
Text funk(Text t) {
// ...
return t;
}
int main() {
Text name("Mark");
Text name2("Knopfler");
name = funk(name);
name = name2;
return 0;
}
So what's causing the error is the function funk, and the first two lines in the main function. I get why it's complaining on the first two lines in the main function, because there are no function called "name" or "name2". But what I'm trying to do is declaring and initialize an object in one line (I'm and old Java guy :p), is this even possible in C++? I can't find anything online indicating this.
The funny thing is that this code is more or less copied from some code my lecturer executes just fine during a lecture. And he has certainly not declared any functions named "name" and "name2" either. Any reasonable explanation for this?
But why is the function funk generating this error as well? All I am doing is returning a copy of the object that I'm sending in.
Thanks in advance!
Edit: Here comes the full error messages. There are five of them. "SecondApplication" is the name of my project.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Text::Text(char const *)" (??0Text##QAE#PBD#Z) referenced in function _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Text::Text(class Text const &)" (??0Text##QAE#ABV0##Z) referenced in function "class Text __cdecl funk(class Text)" (?funk##YA?AVText##V1##Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall Text::operator=(class Text const &)" (??4Text##QAEXABV0##Z) referenced in function _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Text::~Text(void)" (??1Text##QAE#XZ) referenced in function "class Text __cdecl funk(class Text)" (?funk##YA?AVText##V1##Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 5 error LNK1120: 4 unresolved externals C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\Debug\SecondApplication.exe 1 1 SecondApplication
You'll get the link errors (not compilation errors) that you see if you, for instance, forget to add "Text.cpp" to your project so it doesn't get compiled and linked.
There are two errors in the code - one is in the copy constructor, and one is in the assignment operator.
Since the compiler didn't complain about the two errors, I suspect you forgot to add the file to the project.

Passing C++ vector to method results in unresolved externals

I've looked at a ton of examples and I don't know what I'm doing wrong. I can't pass a vector as a parameter or the compiler says unresolved externals.
Here's an example that doesn't work for me:
In the .h:
#include <vector>
void VectorTest(std::vector<std::string> & vect);
In the .cpp:
void VectorTest(std::vector<std::string> & vect)
{
}
Also in the .cpp, in a method where I'm trying to call it:
std::vector<std::string> test;
VectorTest(test);
When I compile I get the error:
error LNK1120: 1 unresolved externals
If I comment out
VectorTest(test);
It builds. I'm still learning C++ so it's probably something obvious. I'm using Visual Studio Express 2013 if that matters.
On a Yahoo! Answers someone posted this example and it does not work for me, same error:
void myfunc( std::vector<int>& vec )
{
}
std::vector<int> vec;
myfunc( vec );
Here's the error (trying to use the myfunc shown above):
error LNK2019: unresolved external symbol "public: void __thiscall trackManager::myfunc(class std::vector<int,class std::allocator<int> > &)" (?myfunc#trackManager##QAEXAAV?$vector#HV?$allocator#H#std###std###Z) referenced in function "public: __thiscall trackManager::trackManager(void)" (??0trackManager##QAE#XZ)
It looks like you declared VectorTest() as a trackManager member function in the header but then defined it as a free function in the cpp. This results in two unrelated functions with the same name. If you try to call VectorTest() without qualifications from inside a trackManager member function, the resolver will (sensibly) prefer the version that is also a trackManager member over the free function. But since that one doesn't have a definition, the linker can't figure out what to do with it and you get an error.
To fix this, either move the declaration out of the body of trackManager (if you want it to be a free function), or write the definition as void trackManager::VectorTest(...) (if you want it to be a member).
You're most likely missing the include file for string. Try to add the following line at the top of the .h file:
#include <string>

Visual studio 2013, unresolved external symbol error when working with local source library (pugixml)

I'm attempting to use the pugiXML library to read some XML, and I can't get it to compile correctly. After moving the source files(3) into the working directory, bringing them into the project, and disabling the use of precompiled headers, I have been having a hard time trying to get it to link and build.
These are the snippets giving me problems:
// XMLAdapter.h
#pragma once
#include "pugixml.hpp"
using namespace pugi;
class XMLAdapter
{
private:
static xml_document doc;
static bool isReady;
static xml_parse_result result;
public:
static void setResource(char* resource);
template <typename T> static T getItems(char* xpath, T (*getThings)(xpath_node_set*));
};
.
// XMLAdapter.cpp
#include "stdafx.h"
#include "XMLAdapter.h"
bool XMLAdapter::isReady = false;
void XMLAdapter::setResource(char* resource){
XMLAdapter::result = XMLAdapter::doc.load_file(resource);
XMLAdapter::isReady = true;
}
and the errors:
Error 1 error LNK2001: unresolved external symbol "private: static
class pugi::xml_document XMLAdapter::doc"
(?doc#XMLAdapter##0Vxml_document#pugi##A) C:\Users\Adam\SkyDrive\Documents\proj\ray\ray\XMLAdapter.obj ray
Error 2 error LNK2001: unresolved external symbol "private: static
struct pugi::xml_parse_result XMLAdapter::result"
(?result#XMLAdapter##0Uxml_parse_result#pugi##A) C:\Users\Adam\SkyDrive\Documents\proj\ray\ray\XMLAdapter.obj ray
Error 3 error LNK1120: 2 unresolved
externals C:\Users\Adam\SkyDrive\Documents\proj\ray\Debug\ray.exe 1 1 ray
I'm fairly new to C++, so any advice would be welcome;

Unresolved external symbol, cannot figure out why

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 ?)