Thread: Does the order of the #include statements matter?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    121

    Does the order of the #include statements matter?

    I'm curious if the order which #include statements are offered in man pages are important.

    For example, man 3 inet_addr has the following #includes:
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    Does it break anything to change the order to:
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/socket.h>

    ? It's mostly an OCD thing, but being able to would help my brain.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't know about these particular ones, but historically there have been cases of libraries with finnicky headers requiring a particular order of inclusion. Best practice is to make headers self-contained such that the order doesn't matter, and you should find that to be the case here.
    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
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    Ty for your response.

    But if there is a problem, will it show during compile time? Or is it a case where you'd have to fully test the program to be sure?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The problems will normally happening during Compile time.

    Edit: I have never had one happen during run-time; I think I can remember one happening during linking.

    Tim S.
    Last edited by stahta01; 04-23-2020 at 09:30 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does declaring classes in the wrong order matter?
    By Swordsalot in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2005, 12:03 AM
  2. Not Recognizing Include Statements?
    By cram in forum C++ Programming
    Replies: 7
    Last Post: 11-18-2004, 11:56 PM
  3. header files, include statements
    By bennyandthejets in forum Windows Programming
    Replies: 3
    Last Post: 01-02-2003, 07:09 PM
  4. FAQ: Header files, include statements
    By bennyandthejets in forum FAQ Board
    Replies: 2
    Last Post: 12-28-2002, 09:10 AM

Tags for this Thread