![]() |
| | #1 |
| Registered User Join Date: Sep 2005
Posts: 16
| header and source files Thanks |
| gtriarhos is offline | |
| | #2 |
| Registered User Join Date: Jun 2005
Posts: 1,436
| Header files and source files are a convention, not an absolute. In practice, header files are typically used to specify the information that is needed to make use of a set of functions (eg how to call the functions, what constant values are passed to functions, structures that need to be populated so they can be passed to functions, etc). Source files tend to include header files, and implement functionality (e.g. call functions that are declared in headers). When you're building a non-trivial program, you will find that you have a number of header files, and those header files will be #include'd in three places 1) In other header files (eg to use functions in header A you need information on types that are described in header B, so header A #include's header B) 2) In source files that make use of the information in the header file (eg uses constant values, uses data types, calls functions) but the compiler does not visibility of how the individual functions are really implemented to be able to call them. An example is the standard library: to do basic I/O you will #include <stdio.h>, but you do not need to know how (say) the fopen() function is implemented - you just need to use it. 3) In source files that implement the functions that are specified in a header file. These source files #include a header file for functions they implement (eg header A specifies the mechanism by which a function is called, and source A #include's header A and then implements the function). These source files will typically include other header files that are needed to implement the required behaviours. THe basic requirements are fairly simple. When a program is built, nothing is allowed to be defined more than once. If the linker sees two bodies for one function (presumably in different object files) it will tend to complain fairly bitterly about things like duplicated symbols. For this reason, the content of header files are usually (mostly) declarations (eg typedefs and function prototypes) while source files are mostly definitions (eg the bodies of functions). In practice, the three classes I've described above are a little arbitrary, and overlaps can occur. For example, macros are often actually implementation details, and may be placed in header files. |
| grumpy is offline | |
| | #3 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,680
| |
| Salem is offline | |
| | #4 | |
| Ultraviolence Connoisseur Join Date: Mar 2004
Posts: 201
| Quote:
| |
| nonpuz is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Confusion on header and source files | dnguyen1022 | C++ Programming | 4 | 01-17-2009 03:42 AM |
| Obtaining source & destination IP,details of ICMP Header & each of field of it ??? | cromologic | Networking/Device Communication | 1 | 04-29-2006 02:49 PM |
| Linking header files, Source files and main program(Accel. C++) | Daniel Primed | C++ Programming | 3 | 01-17-2006 11:46 AM |
| Tutorial review | Prelude | A Brief History of Cprogramming.com | 11 | 03-22-2004 09:40 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |