Thread: Header files

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    1

    Header files


    My textbook said that the
    typical contents of a header file include
    1. a block comment summarizing the library's purpose
    2. #define directives naming constant macros
    3. type definitions
    4. block comments stating hte purpose of each library function and declaration of the form
    extern prototype
    It doesn't mention anything about the definition of the function.

    If the header file includes only the prototypes of a function, how can the compiler know what to do with the function?
    Are functions defined elsewhere?

    This was my question.
    After reading the wiki article on header files, here is my understanding of header files.

    1. it is assumed that the function is defined in one of the relevant source files, which is why the header file does not include the definition. The definition itself is not needed to translate the source code to machine language. The cross-referencing of relevant source files to get the definition of the function is done by the linker.

    2. for a function included in a header file, its prototype is never written in any of the source files, because the prototype is present in the library.

    Is this sound?

    update: further questions

    I built the following files to create my personal library.
    Code:
    file: comp.h (header file)
    
    typedef struct
    {
        double re, im;
    } comp_t;
    
    extern void print_comp(comp_t x);
    Code:
    file: comp.c (implementation file)
    
    #include "comp.h"
    #include <stdio.h>
    
    // displays complex number
    void print_comp(comp_t x)
    {
        printf("%lf + %lfi", x.re, x.im);
    }
    Code:
    file: main.c (user program)
    
    #include "comp.h"
    
    int main()
    {
        comp_t x = {1, 2};
        print_comp(x);
    }
    They are all under the same folder.

    1. My compiler returns
    undefined reference to `print_comp'
    Why? Didn't I #include comp.h, where the function is defined?

    2. The textbook tells me to compile the implementation file each time either the header file or the implementation file is updated. However, my implementation file (and the example in the textbook's) contain uninitialized variables. So wouldn't running them cause errors? When I did compile them, what happened was not getting garbage as a result, but the following error:
    undefined reference to `WinMain@16
    What's happening here?
    edit:
    After researching, I learned that a WinMain@16 error occurs when I don't have a main function (though I still have completely no idea what WinMain is), which is the case in my implementation file. In that case, how am I supposed to compile my implementation file? The example code in the textbook also lacks a main function.

    3. The text book also tells me to include the object files of the user program and the implementation file in the command that activates the linker. Can I do this using my IDE (Code::Blocks), or am I supposed to do this via the command line?
    Last edited by 046; July 20th, 2013 at 10:06 PM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Yes, everything you're being told is correct.

    The linker needs to know what to link in. In most IDEs, you create a project. You then add the source files to that project. When you tell the IDE to build the project, then it will control the compiling of the source files and the linking in of the object files and libraries. The IDE knows what object files to submit to the linker, because you told it which source files belong to the project.

    Every IDE does project management in its own way. I've never used Code::Blocks, so I don't know the details on it. Somewhere in the main menu you should see something about Project, under which you should see something about adding files or objects. Either under Project or under File, there should be a New item that allows you to create a new project; part of that project creation process could be to add files to the project.

    There's an older and well-known form of project management called the makefile which is used in conjunction with the make utility. Indeed, we often refer to "making the project". The philosophy behind make is to create dependencies between the project's files, such that it will only compile the files that have changed since the last make or else depend on a file that has changed since the last build. That philosophy was developed back when computers were still under-powered (by today's standards) and so a complete recompiling of all the files of a non-trivial project would take a rather long time. Modern IDEs still employ that old make philosophy.

    Add the source files to your project and it should work.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    1
    I made a new project, added my header file, implementation file, and user program file.
    I got the desired output from the main program.

    Thanks for the help.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    in codeblock you need to make new project and add both c file in source and then compile.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Don't forget to also add your include file to the project as well.


    Jim

IMN logo majestic logo threadwatch logo seochat tools logo