Original Post
Me and my friend are trying to make a pirate game in C++ to go over the specifics of the language. Was hoping that he could write his part, compiling it into object files (making sure anything I needed was externally linkable), and then writting me a header that externed everything. For this task we decided on Dev Cpp (not looking to change). Needless to say, it isn't working. Here is how everything is setup. There are currently 2 project files, Pirates.dev and PirateCombat.dev. Pirates.dev + pirates.cpp PirateCombat.dev + main.cpp + pirates.h In the PirateCombat.dev file, I went to Project Options > Parameters > Add Library or Object, there I added pirates.o, the object file Pirates.dev makes when I compile it. Here is whats in each file. pirates.cpp - should be stand alone. Makes the variable x available for external linking.
#include <cstdlib>
#include <iostream>
int x = 8;
// Unused except for testing
int main(int argc, char *argv[])
{
system("PAUSE");
return 0;
}
pirates.h - one line, just supposed to pull x from the object file.
extern int x;
pirates.cpp -
#include <cstdlib>
#include <iostream>
#include "pirates.h" // Notice include
using namespace std;
int main(int argc, char *argv[])
{
cout<<x; // x from pirates should be here
system("PAUSE");
return 0;
}
The result of all this is an error about 2 instanes of main, so I'm guessing the first is from the object file and the second from my program. Problem is, If I remove the .o file from my project, I get an undefined refrence to x. Guess what I really need is a way to get what I want, and ONLY what I want, from an object file. Anyone know how to do that? Thanks