Thread: custom include files

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    custom include files

    Why do you need to make your own include files and what do you put in them?

    Thanks
    -Chris

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Why do you need to make your own include files

    You don't need to make your own include files at all. It is perfectly possible to create very large programs without. It is simply that most people, and almost all professionals do not choose to do it that way.

    As your programmes get more complicated, (and larger), you will find you want to break your program up into several smaller files, not least for the fact that it compiles faster. If you change one line in a 100000 line program file, it will compile 100000 lines. If you've broken that up into 1000 line files, only one gets done. This is one reason for custom includes. Rather than re- prototyping and extern'ing your variables in each of your smaller files, your create an include with this stuff in it and #include it.

    Once you get a bit more sophisticated, you'll want to start creating libraries and .DLL's. For the same reason, you'll create custom includes for each.

    As with everything, there are other reasons as well, but think about these for now.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    As I understand it, you write all your functions (and the function's code) into header files, then include those header files into the source file which has the main() function (then the code in the main function). Is this all correct...?

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    A very simple header file might look like this:
    Call it: square.h
    Code:
    #ifndef GUARD_square_h
    #define GUARD_square_h
    
    //Function Prototype
    int Square(int);
    
    #endif
    Its souce file might look this way:
    Call it: square.cpp
    Code:
    #include "StdAfx.h"
    
    int Square(int x)
    {
       return x*x;
    }
    The simple implimentation program would look like this:
    Call it: myprogram.cpp
    Code:
    #include "StdAfx.h"
    #include "square.h"
    #include<iostream>
    using namespace std;
    
    int main()
    {
       cout << Square(10);
       return 0;
    }
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Is this all correct...?

    No. Dean's example is correct. The functions are prototyped in the header, and implemented in other .cpp files.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    actually dean forgot to include square.h in square.cpp where as i understand this might be done in stdafx.h this may not be clear.

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    square.cpp doesn't need square.h.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    actually no but in other cases when its expanded it may. habit forming ya know.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  9. #9
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Cool it can also **** you up....

    When including a header file, you need to judge weather its really needed such as soemthing that has an external class or a class prototype in it, that would be re-defined, he need to understand WHY he' doing it.

    SPH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  2. include files that are'nt in compiler's directory
    By vaibhav in forum C++ Programming
    Replies: 10
    Last Post: 03-25-2006, 11:45 AM
  3. Include files
    By disruptivetech in forum C++ Programming
    Replies: 7
    Last Post: 07-12-2005, 09:52 AM
  4. include files
    By twans in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2004, 07:32 AM
  5. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM