Thread: #include position matters?

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    #include position matters?

    I have read somewhere, it says that include file hierarchy/position matters.
    For example, #include <Winsock2.h> before #include <iphlpapi.h>.
    I would really appreciate if someone could explain this to me. Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It shouldn't, but in practice it can matter if the header file author made some assumptions that other header files would be included first, or if a header tile declares things like macros which may change the meaning of code that follows.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sometimes one header relies upon definitions in a different header. In large systems situations like that do arrise.

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thank you for answering.

    Let's say i have 2 headers.
    winsock.h and winsock2.h

    I include winsock2.h first then only winsock.h. Both of then contain a function named sockfoo() which were defined differently. In this case, which function will be chosen when I call sockfoo() in main(). Is it sockfoo() from winsock.h or winsock2.h ?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by valthyx View Post
    I include winsock2.h first then only winsock.h. Both of then contain a function named sockfoo() which were defined differently. In this case, which function will be chosen when I call sockfoo() in main(). Is it sockfoo() from winsock.h or winsock2.h ?
    Neither. Your compiler will yell at you for having two functions called the same thing in the same scope.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by valthyx View Post
    Thank you for answering.

    Let's say i have 2 headers.
    winsock.h and winsock2.h

    I include winsock2.h first then only winsock.h. Both of then contain a function named sockfoo() which were defined differently. In this case, which function will be chosen when I call sockfoo() in main(). Is it sockfoo() from winsock.h or winsock2.h ?

    Depends on the Compiler/Header files; in this case I think the first one include is used on certain MinGW GCC Compilers.

    How it is sometimes done on MinGW GCC, the header guard for winsock2 declares the header guard for both winsock.h and winsock2.h; never really checked what winsock.h declares.

    http://en.wikipedia.org/wiki/Include_guard

    Tim S.
    Last edited by stahta01; 07-22-2011 at 06:43 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I know it's only an example but you should not need to include both winsock2 and winsock, since winsock2 includes winsock for you.

    The easiest way to track this stuff down is to open the header files in a text editor (NOT a word processor) and have a look for yourself.

  8. #8
    1337
    Join Date
    Jul 2008
    Posts
    135
    Where is the location of the header files (ie. winsock.h) in MS visual studio 2008?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you have your project open in Visual Studio 2008 and you want to see what is in the header files, just right click on the header in the source and a window will pop-up. The first option in that pop-up will say "Open Document <iomanip>" as an example. Choosing that option will open that particular header file in another tab of the IDE so you can see what it contains. No need to go searching for the actual folder where it is located just to view its contents.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include <windows.h> and #include <wininet.h>
    By steve1_rm in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2009, 11:14 AM
  2. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  3. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  4. Replies: 3
    Last Post: 11-03-2002, 02:14 AM