473,614 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ethic on Includes

Jim
Hi:

Do we have some common style for includes when working on a project
with lots of c and h files. Wat I mean is do we have a rule in C when
a file includes several files and those file in turn can call back it.
So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....

su
Nov 13 '05 #1
8 1922
Jim wrote:
Hi:

Do we have some common style for includes when working on a project
with lots of c and h files. Wat I mean is do we have a rule in C when
a file includes several files and those file in turn can call back it.
So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....

su


Officially, there are no rules for h files. The h files are not
necessary or required by the C language standard.

The common convention is to place declarations into header files.
The header files should have "include guards" to protect the
file from being parsed more than once by the compiler. An
example of an include guard:
#ifndef MY_HEADER_H
#define MY_HEADER_H
/* declarations */
#endif /* MY_HEADER_H */

Cyclic definitions should be minimized. Some techniques for
reducing cyclic definitions include "Refactorin g" and forward
declarations. Search the web for "Refactorin g".

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 13 '05 #2
Thomas Matthews <Th************ *************** *@sbcglobal.net > wrote:
Jim wrote:
Hi:

Do we have some common style for includes when working on a project
with lots of c and h files. Wat I mean is do we have a rule in C when
a file includes several files and those file in turn can call back it.
So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....

su


Officially, there are no rules for h files. The h files are not
necessary or required by the C language standard.

The common convention is to place declarations into header files.
The header files should have "include guards" to protect the
file from being parsed more than once by the compiler. An
example of an include guard:
#ifndef MY_HEADER_H
#define MY_HEADER_H
/* declarations */
#endif /* MY_HEADER_H */


It is better to adopt the style:

#ifndef H_MY_HEADER
....

because macros starting with E followed by another uppercase letter are
reserved (so using the other style, you'll have a problem for any header
file name starting with `e').

- Kevin.

Nov 13 '05 #3
Jim wrote:
Hi:

Do we have some common style for includes when working on a project
with lots of c and h files. Wat I mean is do we have a rule in C when
a file includes several files and those file in turn can call back it.
So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....


Having a lot of includes can increase compile time. It's a good idea
to minimize the number of header files that are included in other
header files, or not have any at all. Let's say you have a.h, b.h,
and b.c, and both b.h and b.c are dependent on a.h so both files
include a.h. Everytime you compile b.c, a.h is going to be opened
and parsed twice. If you add another header to b.c that includes a.h,
it's going to be parsed three times. If that header is also dependent
on b.h, then a.h is going to be parsed four times. It only needs to be
read once, so it's a just waste of time to include it all those times.
You can decide not include any headers in other headers. This means
you have to include the headers in the right order in your source files.
a.h would have to be included before b.h in every source file that
needs b.h. So that can be a real pain. You can get around that by
just making one header file with all the headers for the entire project
included in it and just include that in every source file. But then
every file you compile has to read every single header file every
time. If your compiler supports precompiled headers then it will
take of all that in one shot and you don't have to worry about it.
If you don't have precompiled headers and you don't want to include
every file every single time and you don't want to manually arrange
all of the headers for every file you can use external include
guards instead of the usual internal ones. This way, if the file
has already been included, it won't have to be opened at all. The
problem with this is that every time you include a file in a header
you have to wrap it in external guards:

#ifndef A_H
#include "a.h"
#endif
#ifndef B_H
#include "b.h"
#endif

If the code is well organized and modular then you won't really
have to worry about any of this. You just want to avoid including
files dozens or hundreds of times, which can happen if you have a
lot of includes in headers.

In short, the only rule with header files is that you don't want
them to increase compile time unnecessarily.

Matt Gregory

Nov 13 '05 #4

"Jim" <ha********@sif y.com> wrote in message

Do we have some common style for includes when working on a
project with lots of c and h files. Wat I mean is do we have a rule in C
when a file includes several files and those file in turn can call back it. So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....

A structured program should be like a pyramid, with main() and the top and
sub functions getting steadily lower level and more general.
A multiple file program should also have files arranged like a pyramid. If
functions in foo.c call bar.c, bar.c shouldn't call foo.c.
The big debate is whether include files should #include files they are
dependent on. If bar.h defines struct BAR, and foo.h includes a function
that takes a BAR *, then there's a case for making foo.h #include bar.h. The
case against is that the dependency is then obscured.

Another problem is that a small number of functions do need to be mutually
recursive, and occasionally it makes sense to include logically related
functions in the same file, even though this means that the file contains
both caller and callee. In these cases you need to take a hard look at the
code and ask whether it really needs to be organised like that. If it does,
then break the rules.
Nov 13 '05 #5
# Do we have some common style for includes when working on a project
# with lots of c and h files. Wat I mean is do we have a rule in C when
# a file includes several files and those file in turn can call back it.
# So do we keep the includes in the source file or in its header file
# and just call the header from the c file. Are there any more rules
# when we go for includes. Thanx to yu all who bother to share....

I use header files to declare the interface, and source files for the implementation.
The interface would be the bare but complete minimum necessary to exploit the
interface. I include all header files necessary to define types and other objects
necessary to complete the interface.

The source file would include any interfaces it uses. These interfaces are internal
to the implementation and would not be exposed to the interface.

If you include guards in each header
#ifndef some_unique_tag
#define some_unique_tag
...
header file contents
...
#endif
then including the same header multiple times will not cause problems.

Separating interface and implementation makes it simpler to separate concerns.
Also if you build makefile dependencies from the #include, this will help
minimise recompiles if you change the implementation but not the interface.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Elvis was an artist. But that didn't stop him from joining the service
in time of war. That's why he is the king, and you're a shmuck.
Nov 13 '05 #6
Jim
Thanks to u all.. for ur feedback... as for the Refactoring, i really
didnt find much of material for c on the net...

Derk Gwen <de******@HotPO P.com> wrote in message news:<vm******* *****@corp.supe rnews.com>...
# Do we have some common style for includes when working on a project
# with lots of c and h files. Wat I mean is do we have a rule in C when
# a file includes several files and those file in turn can call back it.
# So do we keep the includes in the source file or in its header file
# and just call the header from the c file. Are there any more rules
# when we go for includes. Thanx to yu all who bother to share....

I use header files to declare the interface, and source files for the implementation.
The interface would be the bare but complete minimum necessary to exploit the
interface. I include all header files necessary to define types and other objects
necessary to complete the interface.

The source file would include any interfaces it uses. These interfaces are internal
to the implementation and would not be exposed to the interface.

If you include guards in each header
#ifndef some_unique_tag
#define some_unique_tag
...
header file contents
...
#endif
then including the same header multiple times will not cause problems.

Separating interface and implementation makes it simpler to separate concerns.
Also if you build makefile dependencies from the #include, this will help
minimise recompiles if you change the implementation but not the interface.

Nov 13 '05 #7
ha********@sify .com (Jim) wrote in message news:<e5******* *************** ****@posting.go ogle.com>...
Hi:

Do we have some common style for includes when working on a project
with lots of c and h files. Wat I mean is do we have a rule in C when
a file includes several files and those file in turn can call back it.
So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....

su

You will want to avoid circular references wherever possible. They
may be unavoidable in some situations, but I can't think of an example
off the top of my head. If you have a situation where foo.h includes
bar.h and bar.h includes foo.h, you may want to rethink your design.
If nothing else, create a single foobar.h file and include it in both
foo.c and bar.c (and in any file that uses the functions defined in
foo.c and bar.c).

Personally, I find it best to let header files include whatever files
they need; that way you don't have to worry about the order of
includes in the source file (the header file is said to be
*indempotent*). Since this allows a situation where header files may
be included more than once per translation unit, you'll need to put
guard macros in the header file so that its contents are only parsed
once per translation unit:

#ifndef MY_HEADER_H
#define MY_HEADER_H

/* macros, typedefs, extern var declarations, function prototypes */

#endif /* MY_HEADER_H */

The first time the header file is parsed, MY_HEADER_H hasn't been
#defined, so the contents of the file are parsed. Each subsequent
time the header is parsed in that same translation unit, the
MY_HEADER_H macro will be defined, so the file contents will be
ignored.
Nov 13 '05 #8
John Bode <jo*******@my-deja.com> wrote:
[...]
Personally, I find it best to let header files include whatever files
they need; that way you don't have to worry about the order of
includes in the source file (the header file is said to be
*indempotent*). Since this allows a situation where header files may
be included more than once per translation unit, you'll need to put
guard macros in the header file so that its contents are only parsed
once per translation unit:

#ifndef MY_HEADER_H
#define MY_HEADER_H


I'll just point out, again, that it's probably better to adopt the
convention:

#ifndef H_MY_HEADER
#define H_MY_HEADER

instead, because macros starting with E followed by another capital
letter are reserved, so you wouldn't be able to have a header file
starting with e, follow the former convention and still have a legal C
program.

- Kevin.

Nov 13 '05 #9

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

Similar topics

13
2723
by: Tim Tyler | last post by:
A fairly simple question: I have a library A which depends on library B and C. Currently I have: <?php include("A.inc.php"); include("B.inc.php"); include("C.inc.php");
2
1604
by: Joe Mowry | last post by:
First off, I'm posting this question for a friend that doesn't have access to the news groups. Problem: Using PHP as the base and has several PHP < includes > for various functions, all this by its self works just fine. Failure occurs on adding the XML include. Here's the wierd: If all the PHP includes are removed/commented out,
4
1628
by: Patrick | last post by:
I have an ASP site with about 50 ASP files, all of which are currently including a common "includes.asp" file near the top of the file, responsible for generating the <HEAD/> section of the HTML I need to make a change for 3 of those ASP files such that some dynamic HTML is generated in the <HEAD/> section. I want those dynamic HTML to appear only for 3 specific ASP files. Is there a more elegant way other than to 1) Dim...
4
1769
by: Generic Usenet Account | last post by:
I am seeing some unexpected behavior while using the STL "includes" algorithm. I am not sure if this is a bug with the template header files in our STL distribution, or if this is how it should work. For your benefit, let me first quote from the STL Standard Reference (Stepanov & Lee): template <class InputIterator1, class InputIterator2> bool includes(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2...
12
2981
by: tshad | last post by:
I am not sure why I am getting this error: I have the following code I want to run from another include file that holds all my functions. functions.inc ************************************************************************************ <Script runat="Server"> Sub fnHeader(client As String) response.write("<!-- #include file = ../includes/staffingHeaders.inc -->")
6
2860
by: William F. Zachmann | last post by:
We've got a project going that involves moving an old web site with a massive dll written in C++ that produces most of the output from a SQL 7.0 data base on NT4 onto IIS on Windows 2003 Server with SQL 2000. All new code is being written in C# using ASP.NET and we are using forms authentication to control access to particular directories/applications. We are having a hard time figuring out how to configure the thing so that existing...
3
2528
by: Jeff | last post by:
I have an asp page. Now I know this is sortof OT, but I am not sure of the forum to get an answer. I have an include on this asp page <!-- #include file=\forum\includes\consts-inc.asp --> now, it gives me this error: File attribute '\forum\includes\consts-inc.asp' cannot start with forward slash or back slash.
4
1826
by: Ron | last post by:
Hi all, Trying to work out a few problems in using php on my site. Partially this is a html question. I was reading a lot of the posts and it seems that some of the includes people are using are very complicated because of sessions or id's and variables I don't really know. So... Right now i use php for includes and getting some lists I make using mySQL and phpmyadmin and can get those out without error. For the most part a lot of my...
1
1669
by: JRough | last post by:
I have includes at the top of the file and I have inline includes also includes in mysql queries. For example: include './includes/config.inc.php'; include $include_path.'dates.inc.php'; include $include_path.'auction_types.inc.php'; include $include_path.'countries.inc.php'; include $include_path.'datacheck.inc.php'; include $include_path.'wordfilter.inc.php'; include $include_path.'converter.inc.php';
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8142
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
8642
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
8591
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...
1
8294
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
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.