Thread: Include a header file, but where does the header file look in?

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Include a header file, but where does the header file look in?

    HI,

    If i include a header file, where does the header file ( or more correctly - the compiler ) look in to find the function definitions and stuff that are declared in the header file? Because header file is only for declaring things, the actual code must reside somewhere else? I know the standard libraries reside in the "Include" directory in the compiler's directory. But if i have a custom source file and a header file, i include the header file, but where should i put the custom source file so it can be found by the compiler? The Include dir? Anywhere else?

    thnx thnx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    <header.h>
    This is a header file that comes with your compiler, it can be found in the Include directory. The < > tokens denote this feature.

    "header.h"
    A custom header which you yourself wrote or downloaded somewhere. This header should be placed in the same directory as your source file which uses it, a feature which is denoted by the " " tokens.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should link all the files together with the linker when compiling (*.c/*.cpp files, not the *.h files).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    So... let's say I have a program (main.c) which adds two integers by calling a function sum() :

    Code:
    int sum(int a, int b)
    {
       return a + b;
    }

    Code:
    #include <stdio.h>
    #include "myheaders.h"
    
    int main (void)
    {
       int a = 3;
       int b = 4;
    
       printf ("%d\n", sum (a,b));
    
       return 0;
    }
    where myheaders.h looks like:
    Code:
    int sum (int a, int b);
    shall I just ...

    gcc -c main.c
    gcc -c sum.c
    gcc -o finalprog sum.c main.c

    ???

    thx thx !!!

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Thnx prelude, u answered my question: source file should be placed in the same directory

    But thnx to all guys too who posted.

  6. #6
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up A more polished version...

    A better way of putting preludes answer....(i guess)

    When you #include a header file inclosed in < >, the compiler looks for that header file in the INCLUDE DIRECTORY PATH specified in ur configuration file. But if u include the header file enclosed in " " then the C-Compiler will look for the header file in the current directory. therefore u can also #include ur custom headers in the following way..
    #include"..\myproj\defs.h"

    Hope you got the point...

    Regards,
    Sriharsha.
    Help everyone you can

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i know i know how to include custom header files. I was asking where does the compiler look for the source files for the function definitions and stuff that the custom header files contain, and prelude said in the same directory as the custom header file, and that was the answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cant include libipq.h header file
    By rohit83.ken in forum C Programming
    Replies: 3
    Last Post: 04-04-2008, 02:41 AM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM