Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Question about (Optional)? header files. (http://www.velocityreviews.com/forums/t557344-question-about-optional-header-files.html)

mdh 12-04-2007 08:44 PM

Question about (Optional)? header files.
 
K&R briefly mention splitting programs into seperate files.(page 82)
Using my compiler (X-code) I noticed that creating a header file
gives, as expected one .h file, but creating a new ".c" file creates
an additional file, same name, but with an ".h" extension, and calls
this an optional header file.
Compiling the program using both the .c and .h ( with appropriate
#includes) executes successfully. In K&R, .h files were used to
centralize declarations, etc, and each .c file was used to define
functions etc.
Could anyone explain what is happening here, and perhaps suggest good
usage? ( I looked at the FAQ, and unless I missed it, I did not see
this addressed.)
As usual, thanks in advance.

santosh 12-04-2007 09:16 PM

Re: Question about (Optional)? header files.
 
mdh wrote:

> K&R briefly mention splitting programs into seperate files.(page 82)
> Using my compiler (X-code) I noticed that creating a header file
> gives, as expected one .h file, but creating a new ".c" file creates
> an additional file, same name, but with an ".h" extension, and calls
> this an optional header file.
> Compiling the program using both the .c and .h ( with appropriate
> #includes) executes successfully. In K&R, .h files were used to
> centralize declarations, etc, and each .c file was used to define
> functions etc.
> Could anyone explain what is happening here, and perhaps suggest good
> usage? ( I looked at the FAQ, and unless I missed it, I did not see
> this addressed.)
> As usual, thanks in advance.


Generally header files are used for exporting and importing declarations
and definitions. Say that you have implemented a regular expression
parser library in files r0.c, r1.c and r2.c. Obviously these modules
will need to share some common declarations. A data structure might
need to be global, appropriate typedefs may need to be present and r0.c
may call functions from r1.c, r1.c from r2.c and in other combinations.
Here is where a header file comes into use. You place all the
appropriate declarations into a header, say r.h, and #include this into
all the necessary .c files.

Also your library will export an interface to the world which will need
to be put into one or more headers and supplied along with your source
files.

So, in summary, headers either export some functionality to the outside
world, or import functionality into modules from other modules or serve
as a place to put declarations and definitions that will be used by
multiple modules, like macros and feature test conditionals etc.

The Standard library headers are themselves a good illustration of the
concepts involved. Unfortunately the heavy use of compiler magic causes
most headers from mainstream C libraries to be rather difficult for a
beginner to understand.


Keith Thompson 12-04-2007 09:46 PM

Re: Question about (Optional)? header files.
 
mdh <mdeh@comcast.net> writes:
> K&R briefly mention splitting programs into seperate files.(page 82)
> Using my compiler (X-code) I noticed that creating a header file
> gives, as expected one .h file, but creating a new ".c" file creates
> an additional file, same name, but with an ".h" extension, and calls
> this an optional header file.
> Compiling the program using both the .c and .h ( with appropriate
> #includes) executes successfully. In K&R, .h files were used to
> centralize declarations, etc, and each .c file was used to define
> functions etc.
> Could anyone explain what is happening here, and perhaps suggest good
> usage? ( I looked at the FAQ, and unless I missed it, I did not see
> this addressed.)


This wouldn't be mentioned in the FAQ, because it's not a C issue.

I'm not familiar with X-code, but I suspect it's some kind of IDE, not
just a compiler. Apparently it chooses to generate a .h file for you
in some circumstances. Such a file should be no different from one
you could have written manually.

The compiler itself doesn't care how any soure file was generated.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

mdh 12-04-2007 09:51 PM

Re: Question about (Optional)? header files.
 
On Dec 4, 1:46 pm, Keith Thompson <ks...@mib.org> wrote:
>
> The compiler itself doesn't care how any soure file was generated.
>




Thank you Keith and Santosh.


CBFalconer 12-05-2007 01:38 AM

Re: Question about (Optional)? header files.
 
mdh wrote:
>

.... snip ...
>
> Compiling the program using both the .c and .h ( with appropriate
> #includes) executes successfully. In K&R, .h files were used to
> centralize declarations, etc, and each .c file was used to define
> functions etc.


You are misusing header files. Their purpose is to expose elements
of a .c file that are needed in other .c files. They are, for
example, the method of promulgating the prototype of a function to
other .c files. Things that are not needed externally should not
be mentioned in the .h file, and in fact should often be marked
static in the .c file.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com


Charlton Wilbur 12-05-2007 03:47 PM

Re: Question about (Optional)? header files.
 
>>>>> "KT" == Keith Thompson <kst-u@mib.org> writes:

KT> I'm not familiar with X-code, but I suspect it's some kind of
KT> IDE, not just a compiler.

Xcode (spelling counts!) is Apple's IDE, which uses a customized
version of gcc as its compiler. (I *think* the customizations all
relate to Objective-C, but I wouldn't swear to it.) There's a lot of
DWIM in it.

The best place to ask about Xcode proper is the xcode-users list at Apple.

Charlton


--
Charlton Wilbur
cwilbur@chromatico.net


All times are GMT. The time now is 01:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.