Advertisement

Multi file projects

Started by February 24, 2001 06:41 AM
7 comments, last by Valkyrie 23 years, 11 months ago
Hi all, ive been programming in c++ for a little while now and there is one thing that I can never find any information on nad thats the management of multi file projects. Im used to the one source file and one or more header files but I cant seem to get my mind around larger projects. any help on this would be greatly appreciated. thanks in advance. Valk.
Ok.
Think of it this way:
Header files are the interfaces between source files, and source files are where the actual code goes. Function prototypes for example go in header files, and other source files include this header so they can have access to the function. Variables are declared in the actual source file they are used in, but are "externed" in the header file, meaning extern is added in front of them, which means when another source file includes that header it tells the compiler "this variable is located off in another source file, but you can still use it". You also have to take precaution against multiple inclusion, where one header file would get included more than once and result in redefition errors. You do this by either placing
#pragma once 

at the top of the header, meaning it only gets compiled once, or placing
#ifndef FILE_NAME#define FILE_NAME 

at the top and
#endif 

at the bottom, which basically achieves the same thing.

Ok.. onto an example

File1.cpp will have main() and declare some globals. File1.h will be the header file for this. File2.cpp will declare some functions that File1.cpp will use, and will need access to the globals declared in File1.cpp.

-----
File1.cpp
#include < stdio.h >#include "file1.h"#include "file2.h"int GlobalVar;void main(){    printf("Entering app..."\n);    DoSomething(); //Declared in File2    printf("Ending...\n");} 

File1.h
#ifndef _FILE1_H_#define _FILE1_H_//extern GlobalVar so files that include this can see itextern int GlobalVar;#endif 

File2.cpp
#include "file1.h"#include "file2.h"void DoSomething(){    printf("Inside DoSomething...\n");    GlobalVar = 42;} 

File2.h
#ifndef _FILE2_H_#define _FILE2_H_//prototype the function so files including this can use itvoid DoSomething();#endif 


Phew, that was long. If anything is still unclear just say so .
Advertisement
Multiple files provide three main benefits. The biggest I would say is code reuse. You are highly unlikely to have a use for one entire program within another, but are likely to have use for some part of it. You could simply cut and paste, but if you find a bug or expand it''s functionality you would have to duplicate that change everywhere you copied and pasted it rather than simply recompiling. Explores find command will find all the includes of that file, but finding everywhere you cut and paste it is a matter of how well you kept records and more likely your memory.

The second is that it allows multiple programmers to work on an application at once. This is more of a benefit with DLLs and multiple exe files, but applies to some extent with source. The biggest hinderance is testing, but if the design could work just as well as libraries then where the interfaces are precisely defined then it will still work.

The third is just simple management of the source. Even if you don''t plan to reuse the source or use multiple programmers there is still a benefit to multiple source files. When you get into a large program scrolling to the source you are looking for gets to be a hassle. Your program does not execute sequentially, but when you have only one source file you must arrange the code sequentially. Your program executes as a tree in that one function calls one or more functions that call one or more functions. The entry point is the root of the tree and functions that don''t call any functions are the leaves. Multiple source files allow you to arrange your source in a tree as well. It would be excessive to put each function in a seperate file just the same as it would be excessive to put each statement in a seperate function. Generally a thousand lines is the most I will put into a single file with 500 being the average. That gives me enough to accomplish something significant within a module without having to scroll through a tens of pages of source looking for something.
Keys to success: Ability, ambition and opportunity.
Wow thanks alot guys, that really helped out lots.
Valk.
i still have a problem with mine. well, i don''t know if it''s a real problem, but it works if i don''t include the files in the project(i''m using vc++ 6.0).
i just want to understand why it works if i''m not including it and if i include it it gives 30 errors like
SetupGL.obj : error LNK2005: "bool __cdecl LoadTGA(struct TextureImage *,char *)" (?LoadTGA@@YA_NPAUTextureImage@@PAD@Z) already defined in main.objSetupGL.obj : error LNK2005: "unsigned char * __cdecl makeAlpha(unsigned char *,int,int)" (?makeAlpha@@YAPAEPAEHH@Z) already defined in main.obj 


this is basically nehe''s opengl lesson1 tutorial. since it''s pretty long, i decided to split it up and just put the draw frame function in another file.


can some1 provide a link so i can futher understand it?
or if you want to look at the code yourself, it''s at
http://thuned.dhs.org/programming/demo1.zip


thuned

life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
thuned, you''re on the right track. But, you have a heap of errors Go and read my post above and maybe it''ll be clearer. In SetupGL.h you''re #including SetupGL.cpp - this is a huge no no. You only include header files from .cpp files. Also, you actually have variable declerations in setupgl.h. What you need to do is add "extern" in front of them, then put the actual variable declerations in setupgl.cpp. Also, you don''t need the #ifndef stuff in .cpp files, only in header files, cause they''re the only ones that get included.
Advertisement
Try the tutorial here at GameDev.net by Ben Dilts. I found it to be really good, and he was even kind enough to answer my e-mail I sent him AND look over my source code (but I imagaine he would appreciate it if you kept your source code to yourself, if possible). Its a really good article and I highly suggest reading it!

Never cross the thin line between bravery and stupidity.
quote:
Original post by Quantum

thuned, you''re on the right track. But, you have a heap of errors Go and read my post above and maybe it''ll be clearer. In SetupGL.h you''re #including SetupGL.cpp - this is a huge no no. You only include header files from .cpp files. Also, you actually have variable declerations in setupgl.h. What you need to do is add "extern" in front of them, then put the actual variable declerations in setupgl.cpp. Also, you don''t need the #ifndef stuff in .cpp files, only in header files, cause they''re the only ones that get included.



yeah, i tried that but i couldn''t get it to work
i just wanted to distribute a working version already, not one that wasn''t working and u had to fix everything just to c what it is.

i''ll try it again and read what you said more closely.

thuned


life is unfair, take advantage of it.
UNMB2 - if the link doesn''t work, try clicking it
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
I got it to work fine
I''ll go dig it up and see if I can remember what changes I made.

Ok
main.cpp
Nothing, but you might want to put the function prototypes into a main.h file.

SetupGL.h
1) remove the #include setupgl.cpp
2) The variable declerations. add extern in front of them, and ones that have "= something" remove. e.g.
HDC hDC=NULL;
becomes
extern HDC hDC;
Setupgl.cpp
1)I think this was the file that had #ifndef .. etc at the top, you don''t need that in a .cpp, remove that
2) copy the variables from setupgl.h, don''t put the extern, and put the = there. This is where you''re actually declaring the variables, in the header file you''re just telling the compiler they exist elsewhere.

If you still can''t get it to work paste the errors you get and I''ll see what I can do

This topic is closed to new replies.

Advertisement