About header files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Varahi
    New Member
    • Aug 2012
    • 1

    About header files

    How to create a new header file in c++ and how to input functions into it.
    please tell this in a detailed manner.
    i will be obliged to you.
    Thank you!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Header files are just text files. They are called header files because they are usually included at the top (or head) of the source file.

    Code:
    /* mycode.c */
    #include<afile>
    The name of the header file is not important but usually a header file has a .h in C.

    If you code this:
    Code:
    /* mycode.c */
    #include"afile.h"
    the header file must be in the same folder as mycode.c. If the header file is not there, then it must be in a "standard place".

    If you code this:
    Code:
    /* mycode.c */
    #include<afile.h>
    the header file must be in a "standard place".

    A "standard place" is a set of pre-defined paths you make. Your header file must be some where along one of those paths. How you define a "standard place" varies by compiler so you will need to read your compiler's documentation.
    Last edited by weaknessforcats; Aug 7 '12, 02:08 PM. Reason: #includee typo

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      I try to avoid cpp where I can, but I'll usually do something like

      myheader.h
      Code:
      #ifndef __MYHEADER__
      #define __MYHEADER__
      
      // includes
      #include <iostream>
      #include <string>
      #include <map>
      
      // any user define types ie:
      typedef std::map<std::string, std::string> tdm;
      
      // any function prototypes
      void foo(const std::string &);
      
      #endif
      main.cpp
      Code:
      #include "myheader.h"
      
      int main() {
      
         foo("test");
         std::cin.get();
      }
      foo.cpp
      Code:
      #include "myheader.h"
      
      void foo(const std::string &str) {
         std::cout << str << std::endl;
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        What do you mean by avoiding cpp files where you can?

        Comment

        • divideby0
          New Member
          • May 2012
          • 131

          #5
          Originally posted by weaknessforcats
          What do you mean by avoiding cpp files where you can?
          I meant that I try avoiding the language where I can; I prefer using C becuase it's easier for a layman like myself to understand. :)

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Thanks. I just wanted to be sure you were OK where header files are concerned.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              This #include<"afile .h"> would be a typo and should be #include"afile. h".

              <pedantic>
              Strictly all the standard says is that if you use the form #include<afile. h> the preprocessor searches for a sequence of implementation defined places.

              If you use the form #include"afile. h" the preprocessor searches in an implementation defined manor and if that fails it treats it as if it had been #include"afile. h".

              In every implementation I have seen this boils down to using the <> form searches down a list of paths (the included paths) given either as an environment variable or switches on the compiler command line (or a combination of both) and the "" form searches in the current directory and then down the same path.

              What is not true is that if you use #include"afile. h" then afile.h must be in the same directory as afile.cpp/afile.c

              Generally speaking in most of the projects I work on we use the "" form for any/all project header files and the <> form for standard library header files.
              </pedantic>

              Comment

              Working...