Skip to main content
gamedev.net
🔒 Locked

extern and .o files (may be Dev Cpp specific)

Started by gandolf1212 Feb 19, 2005 at 2:15 PM 3 replies 850+ views
Original Post
gandolf1212
gandolf1212
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
I think there's to much blood in my caffeine system.
Conner McCloud
Conner McCloud
The error tells you what's wrong: you have two instances of main. So get rid of one. Presumably, the one in your friend's pirates.cpp. Don't get rid of pirates.o, just get a copy of pirates.o that doesn't have an instance of main inside it.

Of course, a better solution would be to have him send you his code, and you can include it in your project directly. Why mess with object files if you don't have to?

CM
gandolf1212
gandolf1212
Really just trying to get some practice with it, since I'm having classes on it.

And if main isn't declared in his code, his code wouldn't compile, which means no .o file =/ Maybe there is some way to get around this? DirectX I know is setup like this, only thing I've ever seen like it though.
I think there's to much blood in my caffeine system.
Conner McCloud
Conner McCloud
Quote:
Original post by gandolf1212
Really just trying to get some practice with it, since I'm having classes on it.

And if main isn't declared in his code, his code wouldn't compile, which means no .o file =/ Maybe there is some way to get around this? DirectX I know is setup like this, only thing I've ever seen like it though.

You should be able to build, just not compile. I assume DevCPP gives you the option of building a single file, but I'm not sure. I know in gcc, you just need to add a -c option, but I also don't know how to mess with that in DevCPP.

If you can't figure that out, a work around is to just create two files. 'pirates.cpp' and 'driver.cpp'. All that goes into driver.cpp is main. Then, he should get two object files, pirates.o and driver.o, and the executable. He sends you the pirates.o file, and everything should be OK.

CM
gandolf1212
gandolf1212
Haven't been on for a couple days but I came back, found this, and it worked.

Dev doesn't seem to have a build option but if I just hit compile and wait for it to error (linking step) theres no problem. Your workaround is great too.

Thanks a bunch.
I think there's to much blood in my caffeine system.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.