Thread: a word on preprocessor directives

  1. #1
    Registered User
    Join Date
    Sep 2004

    a word on preprocessor directives

    Hey everyone,
    I was typing out some code when a question popped up in my head that I was dying to have the answer to. Here goes: why is it that when you #include a header file of your own creation (what's the correct terminology? Send a header file to #include?) you write out code like the following:
    #include "whatever.h"
    but when dealing with files that come with your compiler like iostream, you write:
    #include <iostream>
    What exactly is the necessity for the difference in syntax...why arent you able do the following:
    #include <whatever>
    #include "iostream.h"
    Just call this intense curiosity, any insight into this question would be more than appreciated. Thanks a lot-Chap

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > #include "whatever.h"
    Use this when you're including a file which is closely related (in the same directory) as the source file. foo.cpp would typically #include "foo.h".

    > #include <whatever.h>
    There are 2 cases
    1. You've written a library you want to make use of in multiple projects
    2. You've downloaded a library you want to make use of in your projects.
    The <> tells the preprocessor to search all the standard include directories for the file. In most cases, extra files are not in the standard places, so you have to tell it where else to look.
    In gcc, you would use
    gcc -I/path/to/otherlib -I/path/to/mylib foo.cpp
    People using IDE's should look at the project settings, and edit the "additional include directories" field.

    > #include <whatever>
    Is the same as the previous example when it comes to locating the file.
    It may be that files without a .h imply that namespaces are being used, but that's only a guess on my part.
    Certainly any well-written C++ library would use namespaces of some sort or another.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM