#### Archived

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

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

## Recommended Posts

Hi there I want to split my project with separated .cpp, to ease managment, but even after i add a file to the project, compile it, save it and all, it still won't be able to run For example, i can't use a variable in Draw.cpp that as been declare global in main.cpp I tried to #include "Draw.cpp" , but not much luck How can i split my big main.cpp into smaller module using visual c++? thanks -ErayMan [edited by - ErayMan on March 7, 2003 7:56:57 PM]

##### Share on other sites
if you want to include variables into multiple C++ files what you should do is declare them in a .h file like variable.h and then do #include "variable.h" into both your main.cpp and your draw.cpp. that way your calling them from same place so less chance of a mess up and two varibles get assigned with diff values.

##### Share on other sites
This approach may seem a bit confusing, but as you move on to bigger projects with dozens of modules, it really is The Right Thing.

MAIN.CPP : (the main program)

  #include "main.h"          // main header filefloat global_color = 0.5f;int Main(){  DrawSomeBoxes(5);        // draw five boxes  return 1;}

MAIN.H :
  #ifndef _MAIN_H_       // these prevent the main.h file from#define _MAIN_H_       // being included twice - don''t worry                       // about this too much, just put such a                       // definition at the top of each .h fileextern float global_color; // this makes sure this variable can be used in all .cpp files that include main.h#include "draw.h"      // includes draw.h, which contains the                       // variable & function definitions for                       // your drawing code - so they can be                       // used in the rest of the program#endif

DRAW.CPP :
  #include "main.h"      // so globalvariable can be usedvoid DrawSomeBoxes(int number)   // function definition{    for (int i = 0; i<number; i++)    {      // This is where the global variable gets used      glColor3f(global_color,global_color,global_color);      // code that draws a box    }}#endif

DRAW.H :
  #ifndef _DRAW_H_#define _DRAW_H_void DrawSomeBoxes(int number);#endif

Basically the idea is to define variables and functions in your seperate .cpp files, then repeat their definitions in the .h files so that they are known (and can be used) in other parts of the program. Functions and variables that aren''t needed outside their .cpp files don''t need to be put in .h files.

I''m sure there''s better explanations of this online, but this should give you some idea.

##### Share on other sites
Yes, it does hehe
well thanks guys
-ErayMan