You are here:

C++/about header files in c/c++


How can I define user defined header file?

First write the source code you require in the header file and store it in some project related directory. Ensure that the compiler knows about this directory by setting the include path (e.g. for many compilers this is done using the -I or /I compiler command line switch, or if you are using an integrated development environment (IDE), by setting it in the project settings).

To have the compiler (or more strictly, the C/C++ pre-processor) include the text of the header file in your source code, place a pre-processor #include directive in the C/C++ source code files that require it at the point you want the header file text included:

   #include "myheader.h"

Effectively the contents of the header myheader.h will be injected into the source file at the point where the #include directive appears.

If you specify some project include directory and then store headers in sub-directories below this directory then you can include the directory path from that point in the #include directive. So for example if we placed the myheader.h file in directory (on a UN*X or Linux system) /home/me/the_project/include/subdir and we told the compiler pre-processor to look for included files in /home/me/the_project/include the we can include myheader.h like so:

   #include "subdir/myheader.h"

Compilers tend to accept the path in local format but most also accept paths in UNIX format as well (as I used above). On a MS Windows system this could have been:

   #include "subdir\myheader.h"

How a compiler interprets the header file name or path name and how it uses it to locate the header file is 'implementation-defined' meaning it is up to each individual compiler to sort out. I have mentioned common methods such as a -I or /I command line switches to specify paths to search for header files.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.