Thread: header and source files

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    16

    header and source files

    Hi, can someone give me a good explanation of how source and header files work. Can I use function on one source file from another source file? any good links on this? I'm a lost and helpless VB programmer.

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by gtriarhos
    Hi, can someone give me a good explanation of how source and header files work. Can I use function on one source file from another source file? any good links on this? I'm a lost and helpless VB programmer.

    Thanks
    You cant use a header file from a C program in a VB program, if thats what you mean.

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. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM