Thread: Re: [Dev-C++] Where to include files
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-10-11 15:56:23
|
As far as I know there are no "rules". However here is my rule "Include in the .cpp, if it gives you errors put it in the .h". The main reason you don't want to put it in the .h is because the header will be included in EVERY single file that includes your header and (this is the killer) is recompiled every time one of those files is re-compiled. If the header is in the .cpp then it is only re-compiled when the .cpp changes. Of course if you have some super computer avalible then this reason goes out the window. If you don't then this is about the only reason I can think of. Signed David McKen ----- Original Message ----- From: "Glen Vermeylen" <gve...@pa...> To: <dev...@li...> Sent: Saturday, October 11, 2003 10:32 AM Subject: [Dev-C++] Where to include files > Hi, I was wondering where I should best place my include files. > Suppose I have a class testClass which needs to include a couple of files. > Should I place the include-statements in the .cpp files or in the .h (so > one could see which files are needed?)? > Or does it depend on the circomstances? For example: include-files which > contain the type of declared members in the header, and include-files > needed for the implementation in the .cpp file? > Are there rules, or am I free to choose? > > Please enlighten me :-) . > > > > ------------------------------------------------------- > This SF.net email is sponsored by: SF.net Giveback Program. > SourceForge.net hosts over 70,000 Open Source Projects. > See the people who have HELPED US provide better services: > Click here: http://sourceforge.net/supporters.php > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com |
|
From: David M. <ci...@ya...> - 2003-10-11 23:12:52
|
Explain. ----- Original Message ----- From: <age...@co...> To: <ci...@ya...> Sent: Saturday, October 11, 2003 12:12 PM Subject: Re: [Dev-C++] Where to include files > Guard statements can prevent this. > > -- > ~~Scott Simontis~~ > Programmer > > As far as I know there are no "rules". However here is my rule > > "Include in > > the .cpp, if it gives you errors put it in the .h". > > > > The main reason you don't want to put it in the .h is because the > > header > > will be included in EVERY single file that includes your header and > > (this is > > the killer) is recompiled every time one of those files is > > re-compiled. If > > the header is in the .cpp then it is only re-compiled when the .cpp > > changes. > > > > Of course if you have some super computer avalible then this reason > > goes out > > the window. If you don't then this is about the only reason I can > > think of. > > > > Signed > > David McKen > > > > > > ----- Original Message ----- > > From: "Glen Vermeylen" <gve...@pa...> > > To: <dev...@li...> > > Sent: Saturday, October 11, 2003 10:32 AM > > Subject: [Dev-C++] Where to include files > > > > > > > Hi, I was wondering where I should best place my include files. > > > Suppose I have a class testClass which needs to include a couple of > > files. > > > Should I place the include-statements in the .cpp files or in the > > .h (so > > > one could see which files are needed?)? > > > Or does it depend on the circomstances? For example: include-files > > which > > > contain the type of declared members in the header, and > > include-files > > > needed for the implementation in the .cpp file? > > > Are there rules, or am I free to choose? > > > > > > Please enlighten me :-) . > > > > > > > > > > > > ------------------------------------------------------- > > > This SF.net email is sponsored by: SF.net Giveback Program. > > > SourceForge.net hosts over 70,000 Open Source Projects. > > > See the people who have HELPED US provide better services: > > > Click here: http://sourceforge.net/supporters.php > > > _______________________________________________ > > > Dev-cpp-users mailing list > > > Dev...@li... > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > __________________________________ > > Do you Yahoo!? > > The New Yahoo! Shopping - with improved product search > > http://shopping.yahoo.com > > > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: SF.net Giveback Program. > > SourceForge.net hosts over 70,000 Open Source Projects. > > See the people who have HELPED US provide better services: > > Click here: http://sourceforge.net/supporters.php > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com |
|
From: Matthews <dem...@uw...> - 2003-10-12 15:24:03
|
Guard statements are lines like these in your header files: #ifndef FOOHEADER_H #define FOOHEADER_H int foo; char *foostring; //... #endif Then if two files include foo.h, it won't be recompiled because FOOHEADER_H has already been defined. At 04:12 PM 11/10/2003 -0700, David McKen wrote: >Explain. > >----- Original Message ----- >From: <age...@co...> >To: <ci...@ya...> >Sent: Saturday, October 11, 2003 12:12 PM >Subject: Re: [Dev-C++] Where to include files > > >> Guard statements can prevent this. >> >> -- >> ~~Scott Simontis~~ >> Programmer >> > As far as I know there are no "rules". However here is my rule >> > "Include in >> > the .cpp, if it gives you errors put it in the .h". >> > >> > The main reason you don't want to put it in the .h is because the >> > header >> > will be included in EVERY single file that includes your header >and >> > (this is >> > the killer) is recompiled every time one of those files is >> > re-compiled. If >> > the header is in the .cpp then it is only re-compiled when the >.cpp >> > changes. >> > >> > Of course if you have some super computer avalible then this >reason >> > goes out >> > the window. If you don't then this is about the only reason I can >> > think of. >> > >> > Signed >> > David McKen >> > >> > >> > ----- Original Message ----- >> > From: "Glen Vermeylen" <gve...@pa...> >> > To: <dev...@li...> >> > Sent: Saturday, October 11, 2003 10:32 AM >> > Subject: [Dev-C++] Where to include files >> > >> > >> > > Hi, I was wondering where I should best place my include files. >> > > Suppose I have a class testClass which needs to include a >couple of >> > files. >> > > Should I place the include-statements in the .cpp files or in >the >> > .h (so >> > > one could see which files are needed?)? >> > > Or does it depend on the circomstances? For example: >include-files >> > which >> > > contain the type of declared members in the header, and >> > include-files >> > > needed for the implementation in the .cpp file? >> > > Are there rules, or am I free to choose? >> > > >> > > Please enlighten me :-) . >> > > >> > > >> > > >> > > ------------------------------------------------------- >> > > This SF.net email is sponsored by: SF.net Giveback Program. >> > > SourceForge.net hosts over 70,000 Open Source Projects. >> > > See the people who have HELPED US provide better services: >> > > Click here: http://sourceforge.net/supporters.php >> > > _______________________________________________ >> > > Dev-cpp-users mailing list >> > > Dev...@li... >> > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> > >> > >> > __________________________________ >> > Do you Yahoo!? >> > The New Yahoo! Shopping - with improved product search >> > http://shopping.yahoo.com >> > >> > >> > ------------------------------------------------------- >> > This SF.net email is sponsored by: SF.net Giveback Program. >> > SourceForge.net hosts over 70,000 Open Source Projects. >> > See the people who have HELPED US provide better services: >> > Click here: http://sourceforge.net/supporters.php >> > _______________________________________________ >> > Dev-cpp-users mailing list >> > Dev...@li... >> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > >__________________________________ >Do you Yahoo!? >The New Yahoo! Shopping - with improved product search >http://shopping.yahoo.com > > >------------------------------------------------------- >This SF.net email is sponsored by: SF.net Giveback Program. >SourceForge.net hosts over 70,000 Open Source Projects. >See the people who have HELPED US provide better services: >Click here: http://sourceforge.net/supporters.php >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Paul I. <pi...@ot...> - 2003-10-13 17:23:34
|
Matthews <dem...@uw...> wrote: > Guard statements are lines like these in your header files: >=20 > #ifndef FOOHEADER_H > #define FOOHEADER_H >=20 > int foo; > char *foostring; > //... >=20 > #endif >=20 > Then if two files include foo.h, it won't be recompiled because FOOHEAD= ER_H > has already been defined. I think this answer is confusing to all the newbees out there... First point: Of course the header file will be compiled-in with each .cpp/.c that incl= udes=20 it. If you check the makefile, each .cpp is compiled by it self and there= is=20 no way for the compiler to know if the header has already been included a= nd=20 thus compiled... Second point: The guard statements mentioned prevent the same header from being include= d=20 more than once during the compilation of _one_ .cpp=B4. Consider the foll= owing: file1.h #include "file2.h" .... file2.h #include "file1.h" ... Without the guard stetements file1.h would include file2.h whitch include= s=20 file1.h...=20 Third point: The example .h-file above contains definitions of variables. If you inclu= de=20 this file the variables foo and foostring would be instanciated as local = (file- scope) in each .cpp where the .h is included. Not very useful?! To make g= lobal=20 variables declare as _external_ in header fil and move the instansiation = to=20 the .cpp file. Regards, Paul. |
|
From: xRxIxPx <xr...@gm...> - 2003-10-13 21:19:56
|
At 08:15 AM 10/12/2003 -0400, Matthews wrote: >Guard statements are lines like these in your header files: > >#ifndef FOOHEADER_H >#define FOOHEADER_H > >int foo; >char *foostring; >//... That should not happen! If you include this header in two different compile units (means .cpp files), your global objects will be defined in 2 object files making the linker bailing out errornously. In general, you should only have const, static and extern variable declarations in your header files, and in ONE .cpp file the definition for your extern variables. >#endif > >Then if two files include foo.h, it won't be recompiled because FOOHEADER_H >has already been defined. Only true for one compile unit. Better have: #ifndef FOOHEADER_H #define FOOHEADER_H extern int foo; extern char *foostring; //... #endif foo1.cpp #include "fooheader.h" foo2.cpp #include "fooheader1.h" int foo=631; char* foostring=0; Cheers. |
|
From: Per W. <pw...@ia...> - 2003-10-13 21:38:03
|
One note. Since globally declared static variables are only accessible in a single compilation unit, they are normally not declared in any include file. If they are declared in an include file, every C or C++ file that included the file would get it's own copy of a static variable. /Per W On Sun, 12 Oct 2003, xRxIxPx wrote: > In general, you should only have const, static and extern variable > declarations in your header files, and in ONE .cpp file the definition for > your extern variables. > |
|
From: David M. <ci...@ya...> - 2003-10-12 14:13:30
|
Ahh, I see where you are coming from. What those statements won't quard against is compiling files that simply aren't required. e.g. If your class uses math.h you would only include it in the .cpp file. This prevents any .cpp files that include your header from having to compile in math.h (unless they include it themselves). I don't believe the question posed is how to prevent multiple inclusion, if I mis-interpreted then your answer would work perfectly. Signed David McKen --- age...@co... wrote: > Whereever you may be putting your include files, try something like > this: > #ifndef __HEADERNAME_H__ > #define __HEADERNAME_H__ > #endif > > Or, you can use #pragma statements. I am not sure about #pragmas, > as they depend on compilers. Check the MinGW docs. > > -- > ~~Scott Simontis~~ > Programmer > > Explain. > > > > ----- Original Message ----- > > From: <age...@co...> > > To: <ci...@ya...> > > Sent: Saturday, October 11, 2003 12:12 PM > > Subject: Re: [Dev-C++] Where to include files > > > > > > > Guard statements can prevent this. > > > > > > -- > > > ~~Scott Simontis~~ > > > Programmer > > > > As far as I know there are no "rules". However here is my > rule > > > > "Include in > > > > the .cpp, if it gives you errors put it in the .h". > > > > > > > > The main reason you don't want to put it in the .h is because > the > > > > header > > > > will be included in EVERY single file that includes your > header > > and > > > > (this is > > > > the killer) is recompiled every time one of those files is > > > > re-compiled. If > > > > the header is in the .cpp then it is only re-compiled when > the > > .cpp > > > > changes. > > > > > > > > Of course if you have some super computer avalible then this > > reason > > > > goes out > > > > the window. If you don't then this is about the only reason I > can > > > > think of. > > > > > > > > Signed > > > > David McKen > > > > > > > > > > > > ----- Original Message ----- > > > > From: "Glen Vermeylen" <gve...@pa...> > > > > To: <dev...@li...> > > > > Sent: Saturday, October 11, 2003 10:32 AM > > > > Subject: [Dev-C++] Where to include files > > > > > > > > > > > > > Hi, I was wondering where I should best place my include > files. > > > > > Suppose I have a class testClass which needs to include a > > couple of > > > > files. > > > > > Should I place the include-statements in the .cpp files or > in > > the > > > > .h (so > > > > > one could see which files are needed?)? > > > > > Or does it depend on the circomstances? For example: > > include-files > > > > which > > > > > contain the type of declared members in the header, and > > > > include-files > > > > > needed for the implementation in the .cpp file? > > > > > Are there rules, or am I free to choose? > > > > > > > > > > Please enlighten me :-) . > > > > > > > > > > > > > > > > > > > > ------------------------------------------------------- > > > > > This SF.net email is sponsored by: SF.net Giveback Program. > > > > > SourceForge.net hosts over 70,000 Open Source Projects. > > > > > See the people who have HELPED US provide better services: > > > > > Click here: http://sourceforge.net/supporters.php > > > > > _______________________________________________ > > > > > Dev-cpp-users mailing list > > > > > Dev...@li... > > > > > TO UNSUBSCRIBE: > http://www23.brinkster.com/noicys/devcpp/ub.htm > > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > > > > > __________________________________ > > > > Do you Yahoo!? > > > > The New Yahoo! Shopping - with improved product search > > > > http://shopping.yahoo.com > > > > > > > > > > > > ------------------------------------------------------- > > > > This SF.net email is sponsored by: SF.net Giveback Program. > > > > SourceForge.net hosts over 70,000 Open Source Projects. > > > > See the people who have HELPED US provide better services: > > > > Click here: http://sourceforge.net/supporters.php > > > > _______________________________________________ > > > > Dev-cpp-users mailing list > > > > Dev...@li... > > > > TO UNSUBSCRIBE: > http://www23.brinkster.com/noicys/devcpp/ub.htm > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > __________________________________ > > Do you Yahoo!? > > The New Yahoo! Shopping - with improved product search > > http://shopping.yahoo.com > > > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: SF.net Giveback Program. > > SourceForge.net hosts over 70,000 Open Source Projects. > > See the people who have HELPED US provide better services: > > Click here: http://sourceforge.net/supporters.php > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users ===== Signed David Mcken Life Sucks Live with it __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com |