Advertisement

Adding files to projects

Started by March 07, 2003 06:56 PM
2 comments, last by ErayMan 21 years, 11 months ago
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]
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.

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

Advertisement
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.





_________"Maybe this world is another planet''s hell." -- Aldous Huxley
Yes, it does hehe
well thanks guys
-ErayMan

This topic is closed to new replies.

Advertisement