Thread: Devc++ and header files

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    Devc++ and header files

    Hi i created my first header file but cant fathom out how to use it.I have saved it as .h file extension and its in the folder where all my source files are saved.Any tips?

    heres the header file code,with some of the functions just in case the fault is in the code:

    Code:
    #ifndef myfunctions.h
    #define myfunctions.h
    
    
    //playerdam
    int playerdam(int  &gobHealth)
    {
        int dam=diceRoll(10,1);
        gobHealth-=dam;
        return dam;
    }
    
    
    //gobdam
    int gobdam(int  &playerHealth)
    {
        int dam=diceRoll(10,1);
        playerHealth-=dam;
        return dam;
    }
    
    #endif

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    In the main .cpp file you need
    Code:
    #include "name_of_file.h"

  3. #3
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    Thanks was using
    Code:
    #include<myfunctions.h>
    and not
    Code:
    #include "myfunctions.h"
    Also I had the header file in diffent folder to my main source code.
    You live and you learn!

    thanks for the help

    labmouse

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is perfectly valid to have the header in a different folder, but if it's part of your application, then it should use the double-quotes rather than <> to tell the reader "this is one of MY files".

    --
    Mats

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, most header files usually only contain declarations for functions and put the definitions in a .cpp file.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Quote Originally Posted by matsp View Post
    It is perfectly valid to have the header in a different folder, but if it's part of your application, then it should use the double-quotes rather than <> to tell the reader "this is one of MY files".
    Actually, using double quotes means that the path you give it is relative to the project's directory and using <> means it's relative the the compiler's include folder

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by h_howee View Post
    Actually, using double quotes means that the path you give it is relative to the project's directory and using <> means it's relative the the compiler's include folder
    Actually, the rules go something like this:
    • For "file.h": search in compiler-specific directories usually relating to the project. Typically this means ".". If the header file is not found, proceed as if it was <file.h>.
    • For <file.h>: search in compiler-specific directories usually relating to system-wide header files and libraries. Usually this is the standard include directory; however, these paths can often be added to with a command-line parameter. For example, to compile SDL programs you often use -I /usr/include/SDL, which tells the compiler to look for <file.h> in /usr/include/SDL as well as the usual places.

    Anyway, the details aren't really important. Just use "file.h" for header files that you create, and <file.h> for standard system header files like <iostream>. Additional libraries like the SDL can use "SDL.h" or <SDL/SDL.h>. The former is more portable but you have to pass extra parameters to the compiler.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed