Cpp vs header files 
Author Message
 Cpp vs header files

I have some methods I want to seperate from my other files
just to better organize everyhing.  Its just global
methods, not a seperate class or anything.

I can do this by making a new header file and putting them
in there and then including the header file whereever the
methods are needed.

However, I didn't think this is exactly what headers are
supposed to be used for.  Should I be putting these
functions in cpp files somehow?

Also,  sometimes when multiple files try to include one of
the header files I made,  I get a linking error - anyone
know why?



Mon, 08 Dec 2003 21:34:11 GMT  
 Cpp vs header files
To begin with:

The distinction between header files and source files is only
a convenience to provide some organization to the code and
some logical distinctions between source code parts.
(Once you #include a .h file, its text is just the same as if it had
been already in the .cpp file)

What you are describing is more or less exactly what headers
are used for.   But what you should do is create a .h file AND a
.cpp file for your "utility" methods.

The .h file contains the prototypes for the functions, and the .cpp
file will contain the executable code. (Some code can be (and some might
have
to be)  included in .h files, but that's beyond the scope of this answer)

Create myutils.h and myutils.cpp.  You'll include myutils.h in source
modules that use any of the utility methods, and include the .cpp file
in your project files so it gets compiled and linked with your program.

Incidentally, if you use the utils often, and sometimes have other
headers which may themselves use the utils, you need to put
a blocking #if  in the header file e.g.:

//  Start of file
#if !defined(myutils_h)
#define myutils_h
// declarations go here

//end of file:
#endif

This will prevent multiple duplicate declarations in the case of:

A.h:

// some definitions

B.h:

#include "A.h"
// some more definitions

Program.cpp

#include "A.h"
#include "B.h"
// main program here.

As you can see, when compiling Program.cpp, the compiler with run into
the A.h headers at the beginning, then when including B.h, will include A.h
again!  The #if..#endif effectively "jumps around" the source when
encountered
a second (or third or fourth..) time.  (because the first time through it
generated
the myutils_h symbol, which flags the subsequent occurrences to ignore the
enclosed code)


Quote:
> I have some methods I want to seperate from my other files
> just to better organize everyhing.  Its just global
> methods, not a seperate class or anything.

> I can do this by making a new header file and putting them
> in there and then including the header file whereever the
> methods are needed.

    make a myutils.h and a myutils.cpp file.
Quote:

> However, I didn't think this is exactly what headers are
> supposed to be used for.  Should I be putting these
> functions in cpp files somehow?

prototypes in headers, code in cpp's.

Quote:

> Also,  sometimes when multiple files try to include one of
> the header files I made,  I get a linking error - anyone
> know why?

because the actual executables are created in more than one module,
with identical names, so you get duplicate definitions.  You have to
create the method in only one module.  Do this by separating the
declarations from the actual code.


Mon, 08 Dec 2003 22:33:52 GMT  
 Cpp vs header files
No.   Almost never put executable code in header files (or real data storage
for that matter).

The reason is that you'll get multiple copies of the function which the
linker will need to sort out.  The only except to this is templates and
macros, but macros are now discouraged.


Quote:
> I have some methods I want to seperate from my other files
> just to better organize everyhing.  Its just global
> methods, not a seperate class or anything.

> I can do this by making a new header file and putting them
> in there and then including the header file whereever the
> methods are needed.

> However, I didn't think this is exactly what headers are
> supposed to be used for.  Should I be putting these
> functions in cpp files somehow?

> Also,  sometimes when multiple files try to include one of
> the header files I made,  I get a linking error - anyone
> know why?



Mon, 08 Dec 2003 23:31:31 GMT  
 Cpp vs header files
What about inline code?


Quote:
> No.   Almost never put executable code in header files (or real data
storage
> for that matter).

> The reason is that you'll get multiple copies of the function which the
> linker will need to sort out.  The only except to this is templates and
> macros, but macros are now discouraged.



> > I have some methods I want to seperate from my other files
> > just to better organize everyhing.  Its just global
> > methods, not a seperate class or anything.

> > I can do this by making a new header file and putting them
> > in there and then including the header file whereever the
> > methods are needed.

> > However, I didn't think this is exactly what headers are
> > supposed to be used for.  Should I be putting these
> > functions in cpp files somehow?

> > Also,  sometimes when multiple files try to include one of
> > the header files I made,  I get a linking error - anyone
> > know why?



Tue, 09 Dec 2003 03:16:10 GMT  
 Cpp vs header files
thats a whole other thing, and doesn't have anything to do w/ declarations

marc



Quote:
> What about inline code?



> > No.   Almost never put executable code in header files (or real data
> storage
> > for that matter).

> > The reason is that you'll get multiple copies of the function which the
> > linker will need to sort out.  The only except to this is templates and
> > macros, but macros are now discouraged.



> > > I have some methods I want to seperate from my other files
> > > just to better organize everyhing.  Its just global
> > > methods, not a seperate class or anything.

> > > I can do this by making a new header file and putting them
> > > in there and then including the header file whereever the
> > > methods are needed.

> > > However, I didn't think this is exactly what headers are
> > > supposed to be used for.  Should I be putting these
> > > functions in cpp files somehow?

> > > Also,  sometimes when multiple files try to include one of
> > > the header files I made,  I get a linking error - anyone
> > > know why?



Tue, 09 Dec 2003 03:48:18 GMT  
 Cpp vs header files
i just looked it up in the standard because something was bugging me.

An inline function shall be defined in every translation unit in which it is
used.


Quote:
> thats a whole other thing, and doesn't have anything to do w/ declarations

> marc



> > What about inline code?



> > > No.   Almost never put executable code in header files (or real data
> > storage
> > > for that matter).

> > > The reason is that you'll get multiple copies of the function which
the
> > > linker will need to sort out.  The only except to this is templates
and
> > > macros, but macros are now discouraged.



> > > > I have some methods I want to seperate from my other files
> > > > just to better organize everyhing.  Its just global
> > > > methods, not a seperate class or anything.

> > > > I can do this by making a new header file and putting them
> > > > in there and then including the header file whereever the
> > > > methods are needed.

> > > > However, I didn't think this is exactly what headers are
> > > > supposed to be used for.  Should I be putting these
> > > > functions in cpp files somehow?

> > > > Also,  sometimes when multiple files try to include one of
> > > > the header files I made,  I get a linking error - anyone
> > > > know why?



Tue, 09 Dec 2003 20:22:19 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. STL header files vs non-STL header files

2. 2 classes 2 CPP files, no header files

3. Easy ? - Cpp vs Header

4. Header file and cpp file

5. Implementation in header or cpp file?

6. C Header files including header files, mutually referencing typedefs

7. WMF Header Scale ratio vs Aldus Header

8. Public vs Private header files

9. C vs C++ header files

10. header in header files

11. Header files shall not #include any other headers

12. #ifndef/#ifdef problems in my header file (conditional header inclusion)

 

 
Powered by phpBB® Forum Software