Errors LNK2019 and LNK2028 in VC++ Express 2008 - c++

I'm trying to build a solution with 2 projects and get these error messages:
ColliderTest.obj : error LNK2028: undefined token (0A000080) "public: __thiscall Rect::Rect(int)" (??0Rect##$$FQAE#XZ) referenced in function "void __cdecl myFunction(void)" (?myFunction##$$FYAXXZ)
ColliderTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Rect::Rect(int)" (??0Rect##$$FQAE#XZ) referenced in function "void __cdecl myFunction(void)" (?myFunction##$$FYAXXZ)
The code:
In the project "Collider" I have these files:
Collider.h
#pragma once
class Rect{
int x;
int y;
unsigned int w;
unsigned int h;
public:
Rect(int x);
};
Collider.cpp
#include "Collider.h"
Rect::Rect(int x){
this->x = x;
}
The project "ColliderTest" has a reference to the project Collider, and this file:
ColliderTest.cpp
#include "../app/Collider.h"
void myFunction();
void myFunction(){
Rect rect(4);
}
Also, each project has a main.cpp file with an empty main() function, to avoid the complains of the compiller about the entry point.

Both projects have a main function?
That means you are building two executable. Executables typically do not export functions.
You need one executable and one class library (dll).
BTW: If you have an empty main function, how will you know if your program ran?

Related

LNK2019 Error: unresolved external symbol

I am getting an error in Visual Studio when compiling my program.
Error LNK2019 unresolved external symbol "public: __cdecl
Grid::Grid(void)" (??0Grid##QEAA#XZ) referenced in function
main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall
Grid::~Grid(void)" (??1Grid##QAE#XZ) referenced in function
_main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
This project works fine at my university but not on my own computer and I am not sure what is wrong.
My main.cpp:
#include <iostream>
#include "Grid.h"
using namespace std;
int main(int args, char **argv)
{
Grid grid;
// grid.LoadGrid("Grid1.txt");
// grid.SaveGrid("OutGrid.txt");
system("pause");
}
And my header file:
#pragma once
class Grid
{
public:
Grid();
~Grid();
void LoadGrid(const char filename[]);
void SaveGrid(const char filename[]);
private:
int m_grid[9][9];
};
Any help at all is appreciated, thanks.
Issue resolved from advise given on [error LNK2019: unresolved external symbol "public: __thiscall : constructor issue
"First in the library project do rightclick->properties, then under the tab General, Configuration Type should be Static library (.lib)."
Thanks everyone for your answers.
As per my understanding your grid class constructor and destractor implementation are missing. You should check your .cpp file, implemention like this
Grid(){}
~ Grid(){}

Create & Using Dll's On MFC Application

I just created dll project in Visual Studio 2013:New Project->MFC DLL->Next->Check "MFC Extention DLL" and finish.Now, I add new class:
class CMyTest
{
public:
CMyTest();
~CMyTest();
int Test(){ return 1; }
};
Next, I compiled the project and got .lib,.dll files.
In the another project (who using the dll's) I just add the include,lib directory and copy the .dll file to the .exe file location and add the .lib file to Additional Dependency on Linker->Input.
Now, i just create some object from CMyTest class on my OnInitDialog() Method:
CMyTest x;
And when I tried to compiled the project I got Link error:
Error 3 error LNK2019: unresolved external symbol "public: __cdecl CMyTest::CMyTest(void)" (??0CMyTest##QEAA#XZ) referenced in function "protected: virtual int __cdecl CUsingDllProjectDlg::OnInitDialog(void)" (?OnInitDialog#CUsingDllProjectDlg##MEAAHXZ) C:\Users\user\documents\visual studio 2013\Projects\UsingDllProject\UsingDllProject\UsingDllProjectDlg.obj UsingDllProject
Error 4 error LNK2019: unresolved external symbol "public: __cdecl CMyTest::~CMyTest(void)" (??1CMyTest##QEAA#XZ) referenced in function "protected: virtual int __cdecl CUsingDllProjectDlg::OnInitDialog(void)" (?OnInitDialog#CUsingDllProjectDlg##MEAAHXZ) C:\Users\user\documents\visual studio 2013\Projects\UsingDllProject\UsingDllProject\UsingDllProjectDlg.obj UsingDllProject
Where is the problem?
You need to declare the Test method like that (and also the ctr,dctr):
__declspec(dllexport) int Test(){ return 1; }
__declspec(dllexport), instruct the linker to export a symbol to a DLL.you can read about that here: https://msdn.microsoft.com/en-us/library/dabb5z75(VS.80).aspx

LNK2019 Error when compiling .cpp

First, this is hw and I'm supposed to change only one function. But I'm not here for help on that function, I'm here for an LNK error.
Here's the relevant output on the Developer Command Prompt after I type in cl shortest.cpp (with spaces between errors for easier reading):
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::readFile(char *)" (?readFile#Graph##QAEXPAD#Z) referenced in function _main
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::dijkstra(int)" (?dijkstra#Graph##QAEXH#Z) referenced in function _main
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::printPath(int)" (?printPath#Graph##QAEXH#Z) referenced in function _main
shortest.exe : fatal error LNK1120: 3 unresolved externals
I understand this is supposed to mean I haven't defined those symbols, but they should have been predefined for me in one of two files: graph.cpp or graph.h. Graph.h seems more likely, as it contains the declarations of each of these "unresolved" symbols.
graph.h:
#include <iostream>
#include <fstream>
#include <vector>
#include <limits.h>
#include <cstdlib>
#define INFINITY INT_MAX
using namespace std;
class Graph
{
struct Edge
{
int dest; // destination vertex number
int cost; // edge weight
Edge *next;
};
struct Vertex
{
Edge *adj;
bool known;
int dist;
int path;
};
vector<Vertex *> vertices;
vector<int> PQ;
int PQsize;
void PQinsert (int v);
int PQdeleteMin ();
public:
Graph () { PQsize=0; }
int numVertices () { return vertices.size(); }
int dist (int dest) { return vertices[dest]->dist; }
void readFile (char *name);
void dijkstra (int start);
void printPath (int dest);
};
I won't post graph.cpp or shortest.cpp (which only contains the main), unless requested, as it doesn't seem to me that the content of the related functions is an issue. If I do, I will only post the related methods to keep it shorter. But graph.cpp, graph.h, and shortest.cpp are all in the same folder. And shortest.cpp does include graph.h. I'm only supposed to change the dijkstra method, but if I can add something that will make this compile without changing way the methods or class themselves work (which it shouldn't have to) I don't see a problem.
You have to use make file to build with all the source files. Otherwise you can first compile the sources and then link manually. Search google for individual commands for compiling/linking.

error LNK2019: unresolved external symbol error

I'm getting these error messages
2>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CEngine::CEngine(void)" (??0CEngine##QAE#XZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::SetWindowSize(int,int,char const *,int)" (?SetWindowSize#CEngine##QAEXHHPBDH#Z) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::Begin(void)" (?Begin#CEngine##QAEXXZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayWidth(void)" (?GetDisplayWidth#CEngine##QAEHXZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayHeight(void)" (?GetDisplayHeight#CEngine##QAEHXZ) referenced in function _WinMain#16
2>C:\Users\ethan\Desktop\C++ Projects\delveenginetest\Debug\delveenginetest.exe : fatal error LNK1120: 5 unresolved externals
This is my solution:
Solution 'delveenginetest' (2 projects)
DelveEngine
Include
delve.h
Engine.h
SetupSDL.h
stdafx.h
Engine.cpp
Main.cpp
SetupSDL.cpp
This is the code for Engine.h
#pragma once
#include "SetupSDL.h"
class CEngine
{
public:
CEngine(void);
~CEngine(void);
void SetWindowSize(int winW, int winH, const char* GameName, int windowMode);
void Begin(void);
int GetDisplayWidth(void);
int GetDisplayHeight(void);
private:
int deskW;
int deskH;
bool playing;
CSetupSDL* sdl_setup;
};
Code for Engine.cpp
#include "Include/stdafx.h"
#include "Include/Engine.h"
CEngine::CEngine(void)
{
playing = true;
deskW = GetSystemMetrics(SM_CXSCREEN);
deskH = GetSystemMetrics(SM_CYSCREEN);
}
CEngine::~CEngine(void)
{
}
void CEngine::SetWindowSize(int winW, int winH, const char* GameName, int windowMode)
{
// set up SDL for use
sdl_setup = new CSetupSDL(winW, winH, GameName, windowMode);
}
void CEngine::Begin(void)
{
while (playing && sdl_setup->GetMainEvent()->type != SDL_QUIT)
{
sdl_setup->Begin();
sdl_setup->End();
}
playing = false;
}
int CEngine::GetDisplayWidth(void){ return deskW; }
int CEngine::GetDisplayHeight(void){ return deskH; }
The DelveEngine project builds successfully, whereas the delveenginetest project fails.
What's wrong? I've looked everywhere for a reason, can't find one that suits me.
Despite the fact you're not providing all the sufficient information for a correct diagnosis of your problems, I'll try to share what I could imagine that might be the reasons for the linker errors:
I suppose the project delveenginetest you mention is meant to set up unit tests for the classes from the DelveEngine project.
Since you have a Main.cpp in your DelveEngine project, I'd guess it's simply build as an executable (successfully).
Your delveenginetest needs to link to the classes provided from the DelveEngine project, but that's actually not possible, since the .exe from DelveEngine can't be used for linking, you'll need a library to import it to another executable (the unit testing framework).
I'd recommend to separate out your classes/source files from DelveEngine project to make up a static or shared library, that can be linked from an application and the test framework simultaneously from within a single VS solution:
Solution 'DelveEngine' (3 projects)
DelveEngineLib (project [.lib/.dll])
Include
delve.h
Engine.h
SetupSDL.h
Engine.cpp
SetupSDL.cpp
DelveEngine (project [.exe])
Main.cpp
delveenginetest (project [.exe])
Main.cpp (TestFramework main definition)
Since I'm not very versed with it I don't know actually, if VS 2013 supports to setup projects consuming virtual resources (think of links to sources in the actual build environment), but this could be an alternative how to setup application and unit tests without need of an extra library.

How to solve error LNK2019: unresolved external symbol error in Visual Studio 2012 with C++?

I have a visual c++ console project named "C_test".
Another project is called "dll_project" which is dll project.
in "C_test" project, I have set the location of additional include directory for "dll_project".
I have these simple codes.
in "C_test" project's main.cpp.
#include "SampleClass.h"
int main()
{
SampleClass *_parser = new SampleClass();
return 0;
}
And in "dll_project" SampleClass.h and SampleClass.cpp.
SampleClass.h
#pragma once
class SampleClass
{
public:
SampleClass(void);
~SampleClass(void);
};
SampleClass.cpp
#include "SampleClass.h"
SampleClass::SampleClass(void)
{
}
SampleClass::~SampleClass(void)
{
}
When I try to debug or build, I get this error.
error LNK2019: unresolved external symbol "public: __thiscall SampleClass::SampleClass(void)" (??0SampleClass##QAE#XZ) referenced in function _main C:\C_test\main.obj C_test
What have I done wrong?