fatal error LNK1120: 3 unresolved externals

I am a beggenner attempting to actually write C++ program (writing a text based RPG), I'm stuck with a error
"fatal error LNK1120: 3 unresolved externals" and I don't understand it.
I have 3 files in my c++ project

Vitalsma Adventure.cpp
vitalsma.h
path1.cpp

Vitalsma Adventure.cpp:
#include "vitalsma.h"
using namespace std;

int main ()
{
cout << "What is your name, young Warrior?\n";
cin >>name;
cout << "Well, ";
cout << name;
cout << ",\nYou are chosen by us spirits to rid the monsters, restore peace and avenge us.\n\n";
cout << "Take my Broadsword that I used until I perished.\n\n";
cout << "You are walking and the road splits into 3 paths\n";
cout << "Which path will you choose? (type 1, 2 or 3)\n";
cin >> quest1;
if (quest1 == 1);
{
int path1();
{
}
}
system ("PAUSE");
return 0;
}

vitalsma.h:
#include <iostream>
#include <string>
using namespace std;

extern string name;
extern string womanname;
extern short int quest1;

extern int path1();

path1.cpp:
#include "vitalsma.h"
using namespace std;

int path1()
{
cout << "As you enter the first path, you start to have unfamiliar\n";
cout << "thoughts of a Woman whom you know nothing of\n";
cout << "What do you think her name could be?\n";
cin >> womanname;
return 0;
}



If theres anything else I godda fix let me know, keep in mind I'm just starting programming
put all of the files in a single project, so they are compiled together.
The bigger problem here is that you don't understand how coding in C++ works (i.e. you don't understand how the compiler and linker take your text files and make an executable or library). Here are some words I churn out for this case.

Undefined reference means that whilst the compiler understands how the function should work (which means it understands the name of the function, the input types and the return type), the linker cannot find the actual compiled code that actually does the work.

In this case, if you #include <vitalsma.h> in your code, the compiler will then find the function prototype, and from that will know the name and parameters. Good so far. Then, everywhere you use the actual function, the compiler effectively leaves a note to the linker, saying "here, you put in the compiled code that actually makes this function call work".

So, then the linker comes along. The compiler has presented it with some compiled code (but NOT the compiled version of path1.cpp, because you have not compiled that, or if you have, you have not told the linker where to find it). The linker then goes looking for the compiled function. It does not find it. It comes back and says "undefined reference". You fix this by compiling path1.cpp and making sure the linker can find it.

Under g++, this would be done simply:

g++ main.cpp path1.cpp to build.

Under an IDE, commonly you have to add the files to your "project" or whatever your IDE calls groups of files that are meant to be built together.


There is more.

1
2
3
extern string name;
extern string womanname;
extern short int quest1;

extern here is a way to tell the compiler that these variables are created somewhere else. Where? They're not created anywhere in the code you've shown us.
Last edited on
Topic archived. No new replies allowed.