Thread: Including header files

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Including header files

    I apologize if this is a simple question, but I am having trouble including header files into my main program body. For example, I will have all of a library's prototypes in the Library.h file and the implementations of those prototypes in the Library.c file (which includes Library.h). Now, when I try to include Library.h into my Main.c the program is unable to find the function implementations in the Library.c, however, if I include Library.c everything seems to work fine. Why is this so, and am I wrong in thinking I should just be able to include the header file am I missing something here?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    What you have is a linker error - #include-ing Library.h is what the compiler needs so that it knows what functions/global variables exist and what types they have, but they have to be present at link-time (the stage after compilation) if you're to actually use them. Fix: When you compile, specify both Main.c and Library.c on the command-line, like so:

    Code:
    $ cc Main.c Library.c -o Main
    (adjust options for your compiler, obviously)

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Got it working, thanks for the advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header Files / Including
    By FlamingAhura in forum C Programming
    Replies: 2
    Last Post: 11-03-2010, 09:03 PM
  2. Including my own header files
    By bushymark in forum C Programming
    Replies: 17
    Last Post: 11-03-2009, 09:09 PM
  3. including linux header files
    By jacobh in forum Linux Programming
    Replies: 7
    Last Post: 10-19-2009, 03:17 PM
  4. including header files and implementation
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 01-12-2009, 08:59 PM
  5. Including header files
    By Emeighty in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2008, 03:02 PM