error: ld returned 1 exit status

Sep 20, 2014 at 10:02pm
I'm using Ubuntu 14.04 and gcc 4.8.2. I'm trying to compile the following code using this command:

g++ -Wall -W -std=c++11 game.cpp -o game

I'm getting the following error:

game.cpp:(.text+0x24): undefined reference to 'GameLoop::WelcomePlayer(sPlayer&)'
game.cpp:(.text+0x2f): undefined reference to 'GameLoop::RunGame()'
collect2: error: ld returned 1 exit status

Here is the part of my code where I'm assuming my error is:

game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//#include "stdafx.h"
#include "Player.h"
#include "GameLoop.h"

int main(int argc, char* argv[])
{
	sPlayer player;
	GameLoop::WelcomePlayer(player);

	bool bIsPlaying = true;
	while(bIsPlaying)
	{
	  bIsPlaying = GameLoop::RunGame();
	}
	
	return 0;
}


GameLoop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "GameLoop.h"
#include <iostream>
using namespace std;

namespace GameLoop
{
void WelcomePlayer(sPlayer& player)
{
	cout<< "What name shall adorn your gravestone? " << endl;
	
	cin>> player.m_name;

	cout<< endl << "Greetings " << player.m_name << endl;
}

void GivePlayerOptions()
{
	cout<< "What would you like to do? " << endl;
	cout<< "1: Quit" << endl;
}

void GetPlayerInput(string& playerInput)
{
	cin>> playerInput;
}

PlayerOptions EvaluateInput(string& playerInput)
{
	PlayerOptions chosenOption = PlayerOptions::None;

	if(playerInput.compare("1") == 0)
	{
	  cout<< "You have chosen to quit!" << endl;
	  chosenOption = PlayerOptions::Quit;
	}
	else
	{
	  cout<< "I do not recognize that option, try again." << endl;
	}

	return chosenOption;
}

bool RunGame()
{
	bool bShouldEnd = false;

	GivePlayerOptions();

	string playerInput;
	GetPlayerInput(playerInput);

	bShouldEnd = EvaluateInput(playerInput) == PlayerOptions::Quit;

	return !bShouldEnd;
}
}


GameLoop.h
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include "Player.h"
#include "PlayerOptions.h"

namespace GameLoop
{
	void WelcomePlayer(sPlayer& player);
	void GivePlayerOptions();
	void GetPlayerInput(std::string& playerInput);
	PlayerOptions EvaluateInput(std::string& playerInput);
	bool RunGame();
}


Player.h
1
2
3
4
5
6
7
#pragma once
#include <string>

struct sPlayer
{
	std::string m_name;
};


Any help would be appreciated. Thanks.
Sep 21, 2014 at 1:34am
You need to include GameLoop.cpp on the command line otherwise those function definitions you supply won't be linked in.
Sep 22, 2014 at 2:55pm
That did it! Thank you.
Topic archived. No new replies allowed.