Includes in header files?

This is a discussion on Includes in header files? within the C++ Programming forums, part of the General Programming Boards category; I was just wondering what the differencs between putting includes in the header and putting it in the source file, ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question Includes in header files?

    I was just wondering what the differencs between putting includes in the header and putting it in the source file, and when you should use what because it seems that people in general uses both.


    Leiken

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    when you include a header file, the compiler puts the whole file where you typed #include <header.h>

    so
    if header.h was
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    when you do:
    Code:
    #include "header.h"
    
    int main(void)
    {
     printf("Hello, world!");
     return 0;
    }
    it will be
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
     printf("Hello, world!");
     return 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You really don't want to put includes in your header files, the header files are mostly used for declarations so there's no point. If you include the standard headers in your own custom header it just makes the program harder to read and distribute since you don't know what was actually included.
    Code:
    /*
    -------------
    header.h
    -------------
    */
    
    #ifndef _HEADER_H_
      #define _HEADER_H_
      char *function ( int, double );
      int foo ( void );
      double bar ( double );
    #endif
    Code:
    /*
    ----------------
    program.cpp
    ----------------
    */
    #include <iostream>
    #include <string>
    #include "header.h"
    using namespace std;
    
    int main ( void )
    .
    .
    .
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. user created header files
    By MedicKth in forum C++ Programming
    Replies: 6
    Last Post: 09-20-2003, 01:36 PM
  5. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21