Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TheDuderino

DX and multiple files...

This topic is 6501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is driving me nuts.. What i am doing is trying to keep from having 200 pages of source code for a single source file.. but for some reason i cant split stuff up and get it to compile with vc++ 6.0... Even the simple tutorials included int he tutorial i cannot compile if i break them up. What i did is move the global data into a global.h file.. then i also moved all the data manipulation functions into another file and had a header.. so i had main.cpp data.cpp data.h and global.h (and i did use #ifndef) This compiles and says its happy.. but upon linking i get link error2005 object xxx already defined in somethinoranother... How can this be if i used #ifndef stuff??? Please help me out.. its frustrating me to no end.. Russ

Share this post


Link to post
Share on other sites
Advertisement
For global variables (tsk, tsk) you should declare the variable as extern in the header files, and then in one c/cpp file declare the variable again.

ex:
globals.h:

#ifndef _GLOBALS_H_
#define _GLOBALS_H_
extern int num_cows;
#endif

cows.cpp:

#include "globals.h"
int num_cows;

main.cpp:

#include "globals.h"
int main(int argc, char ** argv) {
return num_cows = 0;
}

Share this post


Link to post
Share on other sites
Hmm i will try this when i get in up the morning..
perhaps you can tell me what the command extern does.. that is something i have never used. Seen it but never really used it.

Would i do this for structs also?... ect??

Russ

Share this post


Link to post
Share on other sites
When you have:
extern int num_cows;
it tells the compiler that somewhere out there, there will be a variable of type int called num_cows. You don''t know where it will be yet, but you''re telling the compiler to let the linker worry about it.

If you have
int num_cows;
it tells the compiler to create the variable of type int called num_calls, in the object file that it''s creating, and then let any other object file that comes along that the variable num_cows exists in this particular object file. So if two different cpp files had int num_cows; declared in them, they both will say that they are the sole owner of the variable num_cows, when link time comes along.

And yes, you should also do this with structs, or any other global variables that you share between c/cpp files.

Share this post


Link to post
Share on other sites
When you declare a variable as extern, it means the variable is found in another module. So when you declare it as extern in a header, all the other cpp files include this header, and they know about the variable, but they only know the variable exists in another file. That is why you need one cpp file to actually own the variable.... you get linker errors if you don''t because it can''t find where the variable actually lives
You only need to do this for variables, unless you are creating an actual instance of a struct.
e.g.
  
//Fine:

struct MyStruct
{
int Something;
};

//Not fine:

struct MyStruct
{
int Something;
} MyInstanceOfMyStruct;

//This is because you are declaring MyInstanceOfMyStruct each time

//the header is included


//This will work instead:

extern struct
{
int Something;
} MyInstanceOfMyStruct;



Hope this helps

Share this post


Link to post
Share on other sites
Awesome, i will go through my code and fix that stuff and see if it works. Thanks a ton guys.

Also since im on the subject of my lack of knowledge of c++ is there a way to make a dynamically allocated matrix? Or even is this smart?

Lets say you want to read a file from the map and it gives a map size.. if you could make an array of the map that is the exact size of the map.. it would save memory..

anyway i am not too good with the data structures yet.. but im trying!!

Thanks agaian,
Russ

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!