LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-28-2008, 10:01 PM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
do you find #include order to be arbitrary?


This thread itself might be arbitrary, but I thought of something today that I think will help QC my libraries.

Before today I thought (not too long, though) that #include order was pretty much arbitrary. To make sense of them, as some of my sources have 30 or so, I generally used this pattern:
Code:
#include "header corresponding to this source"

#include <C++ system headers>

#include <POSIX/Linux C system headers>

#include "this library's own API headers"

#include "local headers"
While this makes sense to me and eases my angst for orderly files, aside from the corresponding header, I honestly thought there wasn't any reason to choose one order over another.

Well today I realized that it makes more sense to place the API headers for the library being compiled ahead of the system headers to help QC the API. My reasoning is that a lot of people probably include the system headers first so it might be easy for a missing system #include in an API header to slip by. For example, I have several API headers that require ssize_t, but if the API header is normally included after several POSIX headers it might go unnoticed if I forget to #include <unistd.h>.

Does anyone else see a benefit in changing the order of #includes, or am I just taking "orderly code" too far?
ta0kira
 
Old 08-28-2008, 10:13 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 31
I don't think it matters as long as the header files were designed correctly. Any header file that depends on another header file should include that header file inside itself. You should never have to rely on the programmer to make sure header file A is included because header file B needs it; header file B should have the statement that includes header file A. This way, the programmer knows that the file they include has all the functionality it needs.

Note: the '#ifndef ...#define... #endif' statements make sure that no header file is included more than once. So the order that you explicitly include the header files in may not actually be the real order that the compiler includes them.
 
Old 08-29-2008, 12:02 AM   #3
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
When I'm doing includes, I generally go from most general to most specific. In this way, datatypes (structs, etc.) that are defined are usable in other headers. (Of course, many headers do their own includes, but that's sortof an aside.)

So, I'll often do something like:

Code:
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include "project/types.h"
#include "project/module.h"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where to find include files to C? henrik9 Programming 5 04-13-2006 11:30 AM
How to get gcc to find my 'include' and 'lib'? renshao Linux - General 4 03-11-2006 07:02 AM
g++ V3.3.3 can't find cstdio include on FC2 tommyr Programming 0 06-16-2004 02:30 PM
Where to find/How to change .h include directories for g++ sfcesario Linux - Software 1 01-31-2004 10:12 AM
g++ does not find include files StressedLemming Linux - Newbie 2 11-30-2003 06:12 AM


All times are GMT -5. The time now is 03:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration