Multiple Files Compilation

Hey guys! ^_^

I have tried and tried and googled and googled and everything I have found and tried do not work :(

The files contents are too long to post here, so I just posted links to download them, the header file didn't work when making it available for download, so I am posting the source here.

roleplay.h:
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
/*	Filename: roleplay.h
 *	Devs: Micah Butler (princessjinifer)
 *	Created: 5/10/2010
 *	A text based roleplaying game that I wrote on a boring car trip home from Grandma's house ^_^
 *	v.0 (first build)
*/

//#ifndef ROLEPLAY_H
#define ROLEPLAY_H

#include <cstdlib> // included for the 'system("clear")'

using namespace std; // people say not to add this one, I don't want to type std:: in front of all my things, too much coding :D

string character_name; // at the beginning when they choose their character name, and used later on probably for saved games.*/
string what_to_do; // when the user is asked what they want to do, this is used for their input.
string are_you_sure;
int area_number; // this tells the plan_of_action which area you are in
int gold_amount = 0; // for handling how much gold the player has :)

void game_beginning (); // where the game actually begins
void game_beginning_part2 (); // after the 'plan_of_action' function
void plan_of_action (); // what is your plan of action?
void the_shop (); // After the user enters the shop, this is the function that takes effect
void the_hotel (); // This is the various hotels that the user can go to.
void the_fence (); // the electric fence when you first start off
void the_help (); // shows you the help, I will most likely take this one out
void wanting_to_quit (); // after the user types quit or exit it will take them here asking if they are sure.
void talk_to_phil (); // have a conversation with the first shop owner
void the_shop_part2 (); // where you buy/sell/check prices
void leave_the_building (); // after leaving any building
void the_shop_2 (); // after you talk to phil and use the shop
//void available_actions (); // I am not sure if I will use this one or not, probably not. 


and after compiling with "g++ -c filename" I linked them with "g++ roleplay.o shops.o" and it spits this out at me:

micah@micah-N120:~/C/roleplay$ g++ roleplay.o shops.o
shops.o:(.bss+0x0): multiple definition of `character_name'
roleplay.o:(.bss+0x0): first defined here
shops.o:(.bss+0x4): multiple definition of `what_to_do'
roleplay.o:(.bss+0x4): first defined here
shops.o:(.bss+0x8): multiple definition of `are_you_sure'
roleplay.o:(.bss+0x8): first defined here
shops.o:(.bss+0xc): multiple definition of `area_number'
roleplay.o:(.bss+0xc): first defined here
shops.o:(.bss+0x10): multiple definition of `gold_amount'
roleplay.o:(.bss+0x10): first defined here
collect2: ld returned 1 exit status


Where do I put the strings and ints? I thought in the header, but that didn't work :( any help please? I will answer any question asked if I can :)

Downloads:
roleplay.cpp:
http://ubuntuone.com/p/sxz/

shops.cpp:
http://ubuntuone.com/p/sxx/

(I am running GNU/Linux, so it won't work the same if compiled by Windoze, you will have to change the 'system("clear")' to 'system("cls")')

Thanks in advance!
~princessjinifer
Best way to use globals: don't.
Second best:
1
2
3
4
5
6
7
//In header:
extern T global;

//In one, AND ONLY ONE, source:
T global;
//If you need to, you may initialize it to something or pass parameters to the constructor
//on the above line. 
You can't declare variables in headers.
Best way to use globals: don't.


What are globals?

You can't declare variables in headers.


Okay, I took them out and put them in roleplay.cpp, and it give me errors for them missing in the "g++ -c shops.cpp", if I add them to both, that is just like having them in the header, on my other program, I put all my variables in the header, but that program is just a header and a source file, no others...
Globals are variables declared in global scope (not inside any function).
Do what helios said. (or find a better structure for your program that doesn't rely on globals at all)
Okay, so I would take

1
2
3
4
5
string character_name;
string what_to_do;
string are_you_sure;
int area_number;
int gold_amount = 0;


and make it this?

1
2
3
4
5
extern string character_name;
extern string what_to_do;
extern string are_you_sure;
extern int area_number;
extern int gold_amount = 0;


and then use each of them in one and one source only?
That worked ^_^ Thank you so much! I have tried and tried to do multiple files, and none of the sites I saw showed the source with the extern OR I didn't notice them...
Though there is another way we didn't mention here.
1
2
3
4
5
//header
struct Data{
   static string name;
   static ...
};

1
2
3
4
//one of cpp files
#include "header"
string Data::name;
...

1
2
3
4
5
6
//the other cpp
#include "header"

void functuion(){
   Data::name = "Bob";
}


This allows you to better structure your code, I guess..
I will do that later, I am not into classes yet, I will learn those a little later on, but I will keep this in mine :)
Topic archived. No new replies allowed.