Thread: Making header files

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Making header files

    Hello!!!!! I was reading about header files, and wanted to learn how to make them, because it seems like it's a lot more organized, and helps compile long projects faster. But I don't know the exact name of what I should be searching for. Should i search for preprocessor, inline, or is headerfiles the correct name? A tutorial would be nice.......but just a better understanding of what a header file does would be cool too.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but just a better understanding of what a header file does would be cool too.
    Examine this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout<<my_var;
        
        return 0;
    }
    Will it compile? Nope. The code will produce an error because my_var was not declared. For the code to work, you need to do this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int my_var = 10;
            cout<<my_var;
        
            return 0;
    }
    Next, examine this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	my_func();
        
            return 0;
    }
    Will it compile? Nope. That code will produce an error because my_func() wasn't declared. You can do this to get the code to work:
    Code:
    #include <iostream>
    using namespace std;
    
    void my_func()
    {
    	cout<<"hello from my_func()\n";
    }
    
    int main()
    {
    	my_func();
        
    	return 0;
    }
    Or, if you don't want a bunch of potentially long function definitions cluttering up the top of your file, which makes main() difficult to find, you can do this:
    Code:
    #include <iostream>
    using namespace std;
    
    void my_func();
    
    int main()
    {
    	my_func();
        
    	return 0;
    }
    
    void my_func()
    {
    	cout<<"hello from my_func()\n";
    }
    But, you also have another option for organizing your files. You can remove the function definitions from the main() file and organize them in separate files. For instance, you might want to organize your functions into files called:

    my_functions.cpp

    functions_I_got_from_my_friends.cpp

    functions_I_got_from_my_teacher.cpp

    You can imagine how hard it might be to navigate around a main() file that contained 200 function definitions. You would have to scroll up and down the file hunting for the code you were trying to find.

    Now, suppose your function my_func() is defined in a separate file named my_functions.cpp. If you do this:
    Code:
    int main()
    {
    	my_func();
        
    	return 0;
    }
    you'll get an error just like before because the compiler doesn't know what my_func() is. So, you need a way to put the function declarations above main() somehow--just like in this example:
    Code:
    #include <iostream>
    using namespace std;
    
    void my_func();
    
    int main()
    {
    	my_func();
        
    	return 0;
    }
    
    void my_func()
    {
    	cout<<"hello from my_func()\n";
    }
    You can do that using a header file. The header file should contain all the function declarations of the form:

    void my_func();

    and it will be named:

    my_functions.h

    Then, you can include the header file at the top of the main() file:
    Code:
    #include "my_functions.h"
    
    int main()
    {
    	my_func();
        
    	return 0;
    }
    When your compiler comes across an include statement, it just replaces the include statement with the contents of the file. So, if you have these three files:

    Code:
    my_functions.h
    ------------------
    void my_func();
    Code:
    my_functions.cpp
    -------------------
    #include<iostream> //for cout<<
    using namespace std; //for cout<<
    
    void my_func()
    {
    	cout<<"hello from my_func()\n";
    }
    Code:
    my_program.cpp
    -----------------
    #include "my_functions.h"
    
    int main()
    {
    	my_func();
        
    	return 0;
    }
    after the compiler gets done compiling my_program.cpp, the file will look like this:
    Code:
    void my_func();
    
    int main()
    {
    	my_func();
        
    	return 0;
    }
    The compiler won't complain since it sees the function declaration somewhere before the line where the function is called. However, if you notice, there isn't a function definition after main() like in one of the earlier examples. That's ok, though, because when the definition of my_func() isn't found after main(), something called the linker will examine all the other .cpp files in your project until it finds the definition for my_func().

    To summarize: if you want to organize your function definitions in separate files from main(), create a header file for each .cpp file that contains your function definitions. In the header file, type out the function declarations for each function definition in the corresponding .cpp file. If you have two .cpp files with function definitions, then create two header files with each header file containing the declarations of the functions in the corresponding .cpp file. Then, include each header file at the top of your main() file.
    Last edited by 7stud; 10-16-2005 at 12:38 PM.

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. Making Classes header files.
    By RealityFusion in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2003, 11:54 AM