Thread: Header File Tutorial?

  1. #1
    The One And Only
    Join Date
    Aug 2005
    Posts
    3

    Header File Tutorial?

    I am fairly new to programming for computers. I have noticed that most of the C++ tutorials don't touch on the area of header files a whole lot. Does anyone know where a good header file tutorial is located?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    C++ headers

    But I don't really understand your questions, mind elaborating a bit?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I am fairly new to programming for computers.
    Then, don't worry about header files or use them.

  4. #4
    The One And Only
    Join Date
    Aug 2005
    Posts
    3

    Clearing up info

    Well I'm not really "new" to programming. I've been programming on TI calculators for about 5 years. By new I mean I've only been programming for computers for about a year, which isn't that long to me. I feel I have a fairly good understanding of the basics of C/C++. Sometimes I will look through the include files for Dev-Cpp and I find word and commands that are foreign to me, such as extern, the conditional statements that are preceded by the #, and all of the underscores. I was wondering if there was a tutorial that explains the process of making header files and libraries in detail. I am starting college and taking a programming class 1st semester and I would like to learn as much as possible before I start class as I'm taking and advanced calculus and would like to relieve some pressure.

    Thanks for the help fellow C/C++ programmers.

  5. #5
    Registered User Mark S.'s Avatar
    Join Date
    May 2005
    Location
    England
    Posts
    16
    I think this is a reasonable question. I often wonder what is actually happening when you include header files. I know that it is a means to access functions within the header file but it would be nice to get a more in depth of how the compiler acts upon the

    #include<iostream>

    statement

    also i noticed if you get into the VC98 folder in MSVC++ and look in the include folder there
    you see all the different header files.
    Can i learn anything by clicking on the various header files or not?
    If i can then how about a tutorial on the subject .

    Thanks

    Mark S.

  6. #6
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    I tried to make some guide about header files...
    Maybe i'm not very good at english but try to understand something...

    Quick guide to header files :

    I'll give you one example :

    This is your header file (.h).
    Contents :
    Code:
    // file : myheader.h
    
    void foo()
    {
       // do nothing
    }
    And you have main file (.cpp), where is the main function.
    Contents :
    Code:
    // file : main.cpp
    
    #include "myheader.h"
    
    int main()
    {
        // call the foo function
        foo();
        return 0;
    }
    myheader.h and main.cpp are seperate files.
    Lines that begin with '#' are preprocessor directives.
    Preprocessor reads the code before compiler.
    When it sees line that begins with '#', it executes the command on the right side from the '#' sign.
    In our case this is #include "myheader.h".
    '#include' tells preprocessor to copy whole contents of some file (myheader.h for us) to the file where the '#include' statement is used.

    So, when preprocessor does its job, we have one big file that looks like this :

    Code:
    // combined file that preprocessor makes for compiler.
    void foo()
    {
    }
    
    int main()
    {
        foo();
        return 0;
    }
    I hope this explained you some things about preprocessor and header files.
    If I made mistake somewhere, feel free to insult me.

    You can download sample project here > Download
    It should work on any newer compiler.
    Code:
     
         .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW=	
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I feel I have a fairly good understanding of the basics of C/C++.
    Ok, let's have at it.

    Sometimes I will look through the include files for Dev-Cpp and I find word and commands that are foreign to me, such as extern,
    Defining a variable as extern implies that it's defined somewhere outside the current scope. The extern keyword says to look outside the current block for the definition of the variable. If the variable isn't contained inside a block, then the search for its definition expands to the other files in your program.

    ...the conditional statements that are preceded by the #
    Those are called "preprocessor directives". The preprocessor's job is to prepare your code for the compile phase, whereupon the compiler will process your code into machine instructions. Learning all about the preprocessor directives is like learning another whole language. An example of a preprocessor directive is #define:

    #define pi 3.141

    That tells the preprocessor to substitute the characters "pi" with "3.141" in any lines below the current line when the preprocessor encounters "pi" in your program(except inside comments or in string literals surrounded by double quote marks). You can also use #undefine, which says to stop making those substitutions for the lines of code below the line containing #undefine. If you do this:

    #define pi

    then "pi" will be replaced by nothing, i.e. it will be removed from the lines of code.

    Another very common preprocessor directive is #include. That tells the preprocessor to substitute the contents of the specified file in place of the #include statement. With header files, you may also see the ubiquitous:
    Code:
    #if !defined MY_HEADER_CONTAINING_MY_STUFF_123456789_H
    #define MY_HEADER_CONTAINING_MY_STUFF_123456789_H
    
    //C++ code here
    
    #endif
    That hints at the fact that there is something more going on when you use #define. When you write:

    #define pi 3.141

    pi becomes a "token", and when the token is found in your program it is replaced by 3.141. In other words, pi is some kind of entity that can be #defined or #undefined. That's important because in the line:

    #if !defined MY_HEADER_CONTAINING_MY_STUFF_123456789_H

    you are telling the preprocessor that if the token MY_HEADER_CONTAINING_MY_STUFF_123456789_H hasn't been #defined, then go ahead and continue looking at the next lines between #if and #endif. The next line is:

    #define MY_HEADER_CONTAINING_MY_STUFF_123456789_H

    That says to replace the token MY_HEADER_CONTAINING_MY_STUFF_123456789_H with nothing, i.e remove it from the rest of the lines of you program. You wouldn't expect to find that token anywhere in the lines of your program--all you want to do is #define the token. Once you #define the token, then the if statement will fail the next time you try to include the header file. The effect is that the lines of C++ code between #ifndef and #endif will not be included more than once in a file. Header files contain declarations, and if you include the header file more than once, you will include the declaration of a variable in your file more than once, which is an error. For instance, if you do this in a simple "hello world" program:

    int num = 3;
    int num = 3;

    the compiler will say:

    NO WAY! REDEFINITION OF THE VARIABLE NUM! YOU LOSE! COMPILATION FAILED!!! HAHAHAHA!! GO BACK TO VISUAL BASIC CHUMP!!

    Other preprocessor directives:

    #error -- terminates the preprocessing and displays the specified message. For instance,

    Code:
    #ifndef myFile
    #error "Something went wrong with myFile"
    #endif
    #pragma -- an implementation defined directive, ignored if not recognized

    There are also standard macros defined by the preprocessor, e.g.

    __LINE__ : the line number of the current line as an int
    __FILE__ : the name of the source file as a string literal
    etc.

    and all of the underscores
    I'm not sure what you mean. Underscores can be part of names. I think by convention leading underscores followed by a capital letter are used in C++ libraries, so you don't want to use variable names like that in your programs.

    Why might you want to put your declarations in .h files and your definitions in .cpp files? See this recent thread:

    http://cboard.cprogramming.com/showthread.php?t=68178

    Remember when you include a .h file into another file, the include statement is replaced by the .h file, so any legal C++ you could type at the location of the #include statement can be contained in the .h file.

    I was wondering if there was a tutorial that explains the process of making header files
    1) Name your file with a .h extension.
    2) Put some inclusion guards in the file:
    Code:
    #ifndef SOME_UNIQUE_NAME_H
    #def SOME_UNIQUE_NAME_H
    
    
    
    #endif
    3) Put some C++ between the inclusion guards.
    4) Include the .h file wherever you want those lines of code to appear.

    tutorial that explains the process of making libraries
    Dynamic link libraries(dll's)?? I will defer because I don't know how.



    (...the above was garnered from "Ivor Horton's Beginning C++", chap 10: Program Files and the Preprocessor)
    Last edited by 7stud; 08-04-2005 at 12:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 6
    Last Post: 04-02-2002, 05:46 AM