473,836 Members | 1,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Header include order

It seems like, in every C source file I've ever seen, there has been a
very definite include order, as follows:

- include system headers
- include application headers
- include the header associated with this source file

For example, in a file hello.c:

#include <stdio.h>
#include "utils.h"
#include "hello.h"

(Incidentally I think that a source file which doesn't include the header
file which exports its symbols is _very_ bad, as this is a good way to
check for inconsistencies for free.)

I would argue that the standard order of header including is wrong,
and that the correct order is the reverse. Consider this scenario:

hello.c:
#include <stdlib.h>
#include "hello.h"

hello.h:
struct blah {
size_t size;
};

hello2.c
#include "hello.h"

Inexplicably (from the perspective of the person doing the including)
the file hello.h will cause compiler errors in hello2.c but not in hello.c.
If hello.c were written first, and then the include file used elsewhere,
the error would appear to be "new", and not be caught by those who wrote
hello.c, implementing the functionality exported by hello.h.

If this include order is used, this problem is averted:

- include the header associated with this source file
- include application headers
- include system headers

This is good for two reasons:
1. All headers must now include any system headers they need, and will
fail immediately if they don't.
2. Every header will be included in at least ONE source file before
anything else (the source file associated with that header), allowing
any intra-application dependencies to be caught.

Does anyone have a reasonable justification for the standard include
order that I haven't thought of? Thanks.

--
Derrick Coetzee
Nov 13 '05
60 8328
In <3F************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
I always get tripped up by `offsetof'. For some insane
reason I expect <stdlib.h> to provide it, but of course it's
found only in <stddef.h>.


In practice, offsetof is the only reason for including <stddef.h>.
The other thing defined only by <stddef.h> is ptrdiff_t, which is
seldom used explicitly.

Code needing NULL or size_t is extremely likely to include another
header defining them (<stdio.h>, <string.h> or <stdlib.h>, usually).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #31
Alex wrote:
E. Robert Tisdale wrote:
All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.

If you insist. Solaris 2.6:


I suppose you mean the Sun C compiler
that you got bundled with Solaris 2.6?

/usr/include/stdlib.h:

<...>

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

<...>


I don't mean to quibble but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile

#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.

Nov 13 '05 #32
On Fri, 21 Nov 2003 10:35:03 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Alex wrote:
E. Robert Tisdale wrote:
All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.

If you insist. Solaris 2.6:


I suppose you mean the Sun C compiler
that you got bundled with Solaris 2.6?

/usr/include/stdlib.h:

<...>

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

<...>


I don't mean to quibble


Yes, you do. You just aren't very good at it.
but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile

#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.


Proving that's it's possible to write code which produces errors.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #33
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote:
Alex wrote:
E. Robert Tisdale wrote:
All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.

If you insist. Solaris 2.6: I suppose you mean the Sun C compiler
that you got bundled with Solaris 2.6?
Indeed.

/usr/include/stdlib.h:

<...>

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

<...>

I don't mean to quibble but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile #define _SIZE_T
#include <stdlib.h> size_t size = 32; with your Sun C compiler and show us the diagnostic message
that it issues.


Unfortunately, I am not at liberty to define constants with leading
underscores in their names. That naming convention is reserved for
the implementation.

What is your point, exactly?

Alex
Nov 13 '05 #34
"E. Robert Tisdale" wrote:

I don't mean to quibble but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile

#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.


The compiler is within its rights to do anything at all
(or nothing at all) with this code, since the mis-use of a
reserved identifier invokes undefined behavior. Section 7.1,
paragraph 1, first bullet.

--
Er*********@sun .com
Nov 13 '05 #35
Alex wrote:
E. Robert Tisdale wrote:
Alex wrote:

E. Robert Tisdale wrote:

All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.

If you insist. Solaris 2.6:
I suppose you mean the Sun C compiler
that you got bundled with Solaris 2.6?


Indeed.
/usr/include/stdlib.h:

<...>

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

<...>

I don't mean to quibble but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile

#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.


Unfortunately, I am not at liberty
to define constants with leading underscores in their names.
That naming convention is reserved for the implementation.

What is your point, exactly?


That you are confused.
That you haven't shown that size_t is defined in stdlib.h
instead of in another header file included by stdlib.h.
That your remarks about the ANSI/ISO standard
are irrelevant to my assertion that
"size_t is defined in a header file included by stdlib.h".
That you refusal to publish the diagnostic messages
from you compiler for the above code leads me to suspect
that you already know that size_t is *not* defined in stdlib.h
but in another header file included by stdlib.h

Why are you so dissembling?
Why are you afraid of this simple experiment?

Nov 13 '05 #36
Eric Sosman wrote:
E. Robert Tisdale wrote:
I don't mean to quibble but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile

#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.


The compiler is within its rights to do anything at all
(or nothing at all) with this code, since the mis-use of a
reserved identifier invokes undefined behavior.
Section 7.1, paragraph 1, first bullet.


True but irrelevant.

Nov 13 '05 #37
On Fri, 21 Nov 2003 12:13:14 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Section 7.1, paragraph 1, first bullet.


True but irrelevant.


At long last, I have reluctantly come to the conclusion that you are
irrelevant, and that time reading your posts is wasted. You have the
honor of being only the second person I have ever filtered from this
newsgroup. Bye, now.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #38
"E. Robert Tisdale" wrote:

Eric Sosman wrote:
E. Robert Tisdale wrote:
I don't mean to quibble but
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile

#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.


The compiler is within its rights to do anything at all
(or nothing at all) with this code, since the mis-use of a
reserved identifier invokes undefined behavior.
Section 7.1, paragraph 1, first bullet.


True but irrelevant.


Which part of "undefined behavior" are you having trouble
understanding? If the code produces U.B. (as the sample does),
the implementation' s response is no longer describable in terms
of the Standard. The code is not "C code" because the language
definition assigns it no meaning; it is at best "C with extras."

Back to your original assertion (here re-quoted because
it has long since disappeared in snippage):

Alex>>Where is the definition for size_t?

Tisdale>In a header file included by stdlib.h

This assertion is false in general (although it could be true
for some particular C implementation) . The Standard requires
that `#include <stdlib.h>' must define `size_t' (Section 7.20,
paragraph 2), but does not dictate the mechanism by which the
definition is provided. In particular, it does not require
<stdlib.h> to include some other header file, unmentioned in
the Standard. It does not even require that the inclusion of
<stdlib.h> read a file at all; the compiler can simply "know"
what is supposed to happen, and cause it to happen "magically. "

--
Er*********@sun .com
Nov 13 '05 #39
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote:
Alex wrote:
E. Robert Tisdale wrote:
<snip>
you haven't actually shown that size_t gets defined in stdlib.h
Try to compile
#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

with your Sun C compiler and show us the diagnostic message
that it issues.


Unfortunately, I am not at liberty
to define constants with leading underscores in their names.
That naming convention is reserved for the implementation.

What is your point, exactly?

That you are confused.
That you haven't shown that size_t is defined in stdlib.h
instead of in another header file included by stdlib.h.
That your remarks about the ANSI/ISO standard
are irrelevant to my assertion that
"size_t is defined in a header file included by stdlib.h".
That you refusal to publish the diagnostic messages
from you compiler for the above code leads me to suspect
that you already know that size_t is *not* defined in stdlib.h
but in another header file included by stdlib.h Why are you so dissembling?
Why are you afraid of this simple experiment?


Your reasoning is inane. However, I'll oblige:

$ cat test.c
#define _SIZE_T
#include <stdlib.h>

size_t size = 32;

$ gcc -W -Wall -ansi -pedantic test.c
In file included from test.c:2:
/usr/include/stdlib.h:106: warning: parameter names (without types) in function declaration
/usr/include/stdlib.h:108: warning: parameter names (without types) in function declaration
/usr/include/stdlib.h:109: parse error before `size_t'
/usr/include/stdlib.h:118: parse error before `size_t'
/usr/include/stdlib.h:119: parse error before `)'
/usr/include/stdlib.h:120: parse error before `size_t'
/usr/include/stdlib.h:121: parse error before `)'
/usr/include/stdlib.h:128: parse error before `size_t'
/usr/include/stdlib.h:129: parse error before `size_t'
/usr/include/stdlib.h:132: parse error before `mbstowcs'
/usr/include/stdlib.h:132: parse error before `size_t'
/usr/include/stdlib.h:132: ANSI C forbids data definition with no type or storage class
/usr/include/stdlib.h:133: parse error before `wcstombs'
/usr/include/stdlib.h:133: parse error before `size_t'
/usr/include/stdlib.h:133: ANSI C forbids data definition with no type or storage class
test.c:4: parse error before `size'
test.c:4: warning: type defaults to `int' in declaration of `size'
test.c:4: ANSI C forbids data definition with no type or storage class
$

Happy?

Alex
Nov 13 '05 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6434
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file? What is the best followed approach?
7
2096
by: header_question | last post by:
Is it best to place all #include files in one header file seperate from all the other files in a project? What is the logic that determines the order of the #include files, does it depend on the order of function calls in main?
11
2766
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
3
3101
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by using header file. i can create a class c1 with f1() in c1.h and include this c1.h in file1.cpp by giving the statement #include "c1.h" tell me that what exactly is the difference between c1.h and c1.cpp? Since they both are doing the same things.
8
3054
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
3
2751
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex inclusion relationships. Another question I have is how to solve this dependency when "typedef" is used. usually we can use forward declaration like "struc A; " to solve references like "struct A* p". But in many places we have
12
2100
by: Ben | last post by:
I'm kind of new to creating templates. I've made some small class and function templates in the past and I have used quite of bit of the STL, but I am having problems tyring to create templates. I'm trying to make class templates in seperate header files with pointers to each other, and I am getting strange errors. Is it possible to do?
1
1754
by: Marco Spatz | last post by:
Hi, I used PCLint to check my code yesterday and got some warnings about "Repeated include files" and "Redundant declaration for symbol 'CLASSNAME'". I know the reasons for these warnings and I think they are not a problem. But all this made me think about the order in which header files are include. Think about a 4 layered architecture with
11
2813
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp) inside a class with out declaring those functions in the class declaration in the header file? thanks
0
9666
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10836
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7786
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6976
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4009
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.