Archived

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

[C++] Dividing a Source File (basic .h/.cpp type question)

This topic is 5149 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

Hi. I know C++ pretty well/thoroughly, as in, well enough to use, but I don''t have very much experience, so it''s always a challenge to get things set up and all. My question is about dividing a set of functions that would normally be in one Source File (.cpp) into two Source Files. Say I have Source1.cpp. It''s sort of cluttered, and I want to separate all the ugly Windows handling junk from the pretty OpenGL stuff. As the project progresses I will want to make more divisions, but this is the first, basic division I want to make. So, I make a file Source2.cpp. I cut the one portion from Source1.cpp and paste it into Source2.cpp, and leave the rest in Source1.cpp. These two Source Files share the same global variables, call each others'' functions, and require the same "#include <...>"''s to work. I know this won''t compile just by itself. So I make a Source1.h (it''s just called Source1 for readability; it''s not actually a source file) and a Source2.h. Now I have a problem. In which header file do I put my "#includes" (for the various external OpenGL and Windows stuff)? I can''t put it in both, because I don''t want to have it twice (that would cause errors, wouldn''t it?). And in which do I define my Globals? It would be much easier if I could have one Header file linking the two Source files to the Globals and Function Declarations and all, but they can''t both include one Header file. So what am I to do? I just need a general answer, like "Put the globals in a header file and have one include it and ...", not "Header files contain function and class declarations, while Source Files contain implementations and main project code." I was able to get something like this to work, but it was mostly just guessing and checking, putting random "extern" keywords everywhere, etc... I would like to know how this kind of modularization is achieved. Thanks for your help!

Share this post


Link to post
Share on other sites