Simple program gets LNK2019: unresolved external symbol Visual Studio 2013 - c++

I've been struggling with this error on a more complex solution for days, so I simplified a test project to see if I can't figure it out and it still gets the same error:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> Test.cpp
1> MainThing.cpp
1> main.cpp
1> Generating Code...
1>MainThing.obj : error LNK2019: unresolved external symbol "public: static int __cdecl Test::add(int)" (?add#Test##SAHH#Z) referenced in function "public: void __thiscall MainThing::run(void)" (?run#MainThing##QAEXXZ)
1>P:\Leif's Documents\Visual Studio 2013\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
MainThing.h
#pragma once
class MainThing
{
public:
MainThing();
~MainThing();
void run();
private:
int result;
};
MainThing.cpp
#include "MainThing.h"
#include "Test.h"
#include <cstdlib>
#include <iostream>
MainThing::MainThing()
{
}
MainThing::~MainThing()
{
}
void MainThing::run() {
int result = Test::add(5);
std::cout << result;
}
Test.h
#pragma once
class Test
{
public:
static int add(int a);
};
Test.cpp
#include "Test.h"
int add(int a){
int b = a;
int c = 5;
int d = b + c;
return d;
}
main.cpp
#include <iostream>
#include "MainThing.h"
int main(int argc, char** argv){
MainThing mainGame;
mainGame.run();
return 0;
}
This has been super frustrating and the fact that it even effects a fairly simple recreation test without any external dependencies makes it that much more so.

I think you just have a typo. Test.cpp should be
#include "Test.h"
int Test::add(int a){
int b = a;
int c = 5;
int d = b + c;
return d;
}
The way you have, you've written a free function that other things in Test.cpp could use, but no one else knows about. The compiler is looking for Test's method called add and isn't finding one.

Related

How to use visual studio code (MSVC) to compile multi-cpp file?

I have followed some instructions to construct Visual studio code C/C++ compile and debug environment. But MSVC compiler can only compile the selected cpp file, so the included .h file associated the cpp file can not compiled. then the terminal shows
*
main.obj : LNK2019 error: reference to an unresolved external
character "int __cdecl func(void)" (?func##YAHXZ) in the main
function.C:\TESTmsvc\main.exe : fatal error LNK1120: unresolved
external elements: 1
The code as below:
the a.h file
int func();
the a.cpp file
#include <iostream>
#include "a.h"
using namespace std;
int func(){
return 111;
}
the main.cpp file
#include "a.h"
using namespace std;
int main()
{
int b = func();
cout << b << endl;
}

Linker issue in Visual Studio C++

I'm trying to get started with C++ in VS 2017 (empty project template), but immediately ran into linker problems when adding 1 simple class, so I guess I'm missing something important...
My project looks like this:
test.h:
#include <iostream>
class test
{
public:
test();
~test();
std::string getInfo();
};
test.cpp:
#include "test.h"
test::test() {}
test::~test() {}
std::string getInfo() {
return "test";
}
And main.cpp:
#include <iostream>
#include <string>
#include "test.h"
int main(int argc, char **argv) {
test t;
std::cout << "output: " << t.getInfo() << std::endl;
return 0;
}
The linker error I get is the infamous LNK2019:
LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl test::getInfo(void)" (?getInfo#test##QEAA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) referenced in function main
Any ideas what I am doing wrong here?
Thanks!
In file test.cpp you need to properly specify the scope of the member function:
std::string test::getInfo() {
return "test";
}
Note the test:: before the getInfo()

Unresolved external symbol referenced in function error

I've looked at other people's solutions to this problem and none of them, unfortunately, have solved my issue. I was having this error in my dev project, so I started from scratch, followed this tutorial to the letter, including the file names and locations, and I am still getting
1>unittest1.obj : error LNK2019: unresolved external symbol "public:
static int __cdecl HelloWorld::getTwo(void)"
(?getTwo#HelloWorld##SAHXZ) referenced in function "public: void
__thiscall UnitTest1::UnitTest1::TestMethodGetTwo(void)" (?TestMethodGetTwo#UnitTest1#1#QAEXXZ)
1>unittest1.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
HelloWorld::getN(void)const " (?getN#HelloWorld##QBEHXZ) referenced in
function "public: void __thiscall
UnitTest1::UnitTest1::TestMethodGetN(void)"
(?TestMethodGetN#UnitTest1#1#QAEXXZ)
The code is in the tutorial I linked but I will copy it below. I don't understand what I could be doing wrong at this point - my test project depends on my build project, my definitions for these functions are in the class.
HelloWorld.h
#pragma once
class HelloWorld
{
int n = 10;
public:
static int getTwo();
int getN() const;
};
HelloWorld.cpp
#include "stdafx.h"
#include "HelloWorld.h"
int main()
{
return 0;
}
int HelloWorld::getTwo()
{
return 2;
}
int HelloWorld::getN() const
{
return n;
}
unittest1.cpp (in test project)
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../HelloWorld/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethodGetTwo)
{
Assert::AreEqual(2, HelloWorld::getTwo());
}
TEST_METHOD(TestMethodGetN)
{
HelloWorld hw = HelloWorld();
Assert::AreNotEqual(0, hw.getN());
}
};
}
For some reason the linker can't find my definitions, and I'm out of ideas about why that might be.

C++ Visual Studio 2015 - Creating a DLL and Using it in Console Application

All,
I am trying to do a very simple task that in C# is a simple routine, however I am currently trying to get familiar with C++ by creating a DLL and referencing it in a Win32 Console Application. I've honestly followed every step described in https://msdn.microsoft.com/en-us/library/ms235636(v=vs.140).aspx in a lot of detail, however I still get an error as shown below:
1>------ Rebuild All started: Project: MathFuncsDll, Configuration: Debug Win32 ------
1> stdafx.cpp
1> dllmain.cpp
1> MathFuncsDll.cpp
1> Creating library C:\Users\Dilmer\documents\visual studio 2015\Projects\DynamicLibrary\Debug\MathFuncsDll.lib and object C:\Users\Dilmer\documents\visual studio 2015\Projects\DynamicLibrary\Debug\MathFuncsDll.exp
1> MathFuncsDll.vcxproj -> C:\Users\Dilmer\documents\visual studio 2015\Projects\DynamicLibrary\Debug\MathFuncsDll.dll
2>------ Rebuild All started: Project: MyExecRefsDll, Configuration: Debug Win32 ------
2> stdafx.cpp
2> MyExecRefsDll.cpp
2> Generating Code...
2>MyExecRefsDll.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static double __cdecl MathFuncs::MyMathFuncs::Add(double,double)" (__imp_?Add#MyMathFuncs#MathFuncs##SANNN#Z) referenced in function _main
2>C:\Users\Dilmer\documents\visual studio 2015\Projects\DynamicLibrary\Debug\MyExecRefsDll.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
Here's my DLL Project Called MathFuncsDll:
MathFuncsDll.h
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
namespace MathFuncs
{
class MyMathFuncs
{
public:
static MATHFUNCSDLL_API double Add(double a, double b);
static MATHFUNCSDLL_API double Substract(double a, double b);
static MATHFUNCSDLL_API double Multiply(double a, double b);
static MATHFUNCSDLL_API double Divide(double a, double b);
};
}
MathFuncsDll.cpp
#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
MATHFUNCSDLL_API double Add(double a, double b) {
return a + b;
}
MATHFUNCSDLL_API double Substract(double a, double b) {
return a - b;
}
MATHFUNCSDLL_API double Multiply(double a, double b) {
return a * b;
}
MATHFUNCSDLL_API double Divide(double a, double b) {
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
}
Here's my console application MyExecRefsDll project:
MyExecRefsDll.cpp
#include <iostream>
#include "MathFuncsDll.h"
using namespace std;
int main()
{
double a = 7.5;
int b = 99;
MathFuncs::MyMathFuncs::Add(a, b);
return 0;
}
I've also added ..\MathFuncsDll\ to Additional Include Directories. Let me know if you can think of some options, I haven't been able to get this to work and I don't think it should be as difficult as this is.

First c++ file and header LNK2019 error

This is my first time having separate files and first time writing a header file, however I keep getting the same error I can't fix. Here are the files:
//main.cpp
#include <iostream>
#include "Bike.h"
/*
class Bike{
public:
int tyreDiameter;
int getTyreDi(){
return tyreDiameter;
}
}; */
int main(){
Bike b;
b.tyreDiameter = 50;
std::cout << b.getTyreDi();
while (1){
continue;
}
return 0;
}
//Bike.cpp
class Bike{
public:
int tyreDiameter;
int getTyreDi(void){
return tyreDiameter;
}
};
//Bike.h
#ifndef BIKE_H
#define BIKE_H
class Bike{
public:
int tyreDiameter;
int getTyreDi(void);
};
#endif
Now if I have only one file and use the class that is commented out in main.cpp everything works fine. But as soon as I try to separate the Bike class into another cpp file I get this error:
Error 1 error LNK2019: unresolved external symbol "public: int
__thiscall Bike::getTyreDi(void)" (?getTyreDi#Bike##QAEHXZ)
Error 2 error LNK1120: 1 unresolved externals
I am using Microsoft Visual Studio 2013.
Any help would be much appreciated
Why are you defining class Bike twice? in the cpp and in the h, the correct way would be this:
header
//Bike.h
#ifndef BIKE_H
#define BIKE_H
class Bike{
public:
int tyreDiameter;
int getTyreDi(void);
};
#endif
cpp
//Bike.cpp
#include "Bike.h"
int Bike::getTyreDi(void)
{
//implementation like return tyreDiameter;
}