LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-12-2004, 03:06 PM   #1
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
Organizing C code: .c files vs .h


Hi,

Whats the difference between having the file a.h included in file b.c and then compiling b.c, and having a.c and b.c where the functions to be used in b.c are declared "extern", compiling them separately and then linking? Is there any performance benefit associated with either method?

I'm curious to know.

M
 
Old 01-12-2004, 03:14 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I don't know of any performance benefit, but it is pretty much expected that when you do a large scale project in C you will have header files defining the functions and .c files implementing them. This allows somebody to pull up the header files and read the comments and definitions of the functions in order to use them without having to wade through all the source code when they don't particular care how a function works, so long as it does.
 
Old 01-12-2004, 03:55 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
There's definitely no runtime performance benefit. All of that stuff is handled during compiling and should generate the same executable file. The only performance benefit I can see is the tiny drive seek time to read in the header file.

A very small price to pay for more code readability
 
Old 01-12-2004, 04:18 PM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
It's merely a matter of separating interface from implementation. Interfaces go into the header (.h) files, and implementations go into the source (.c) files.
 
Old 01-12-2004, 04:23 PM   #5
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
So its only a convention then - compilers don't treat .c and .h files differently?

I notice that strange things happen if you try to #include a .c file though, so theres at least 1 difference... Maybe there are more?
 
Old 01-12-2004, 04:53 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
A #include simply inserts all of the file contents into the spot where your #include is.

For instance, if you have a file called foo.h and it had this in it:

#define FOO

And then you had a bar.c file that looked like this:

#include "foo.h"

int main(void) { return 0; }

Then the compiler just sees:

#define FOO

int main(void) { return 0; }
 
Old 01-12-2004, 10:41 PM   #7
stoe
LQ Newbie
 
Registered: Jan 2004
Location: ny
Distribution: gentoo
Posts: 8

Rep: Reputation: 0
externing functions and variables instead of using header files is considered bad practice nowadays and should be avoided if possible. the reasons this is the generally recommended way of doing things is as follows:

1) if you extern a function or variable everywhere its used, then if the prototype needs to change, not only will you have to modify the .c file containing the definition and (most likely) where its used, but also every extern statement as well. so it saves in time/energy in maintainance and reduces complexity.

2) using header files promotes modularity by separating interface from implementation. it promotes better design and helps keep code that should be distinct from blending together. if you have to look through the .c file for functions to extern, you'll be more likely extern stuff that shouldn't really be exposed outside the defining module. if you're working on the project with other people, you/someone might extern something that's not expected and then when the implementation of the defining module changes, the code either fails to compile or worse has a new bug.

3) there aren't (at least none that i'm aware of) any real benefits, speed or otherwise, of using externs vs header files.

externs can be useful but should only be used when nothing else will do.

Last edited by stoe; 01-12-2004 at 10:42 PM.
 
Old 01-12-2004, 10:49 PM   #8
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
The reason your getting an error when including the .c file is probably your compiling the .c file as well, so their are conflicts between it and the file its included in.

The main advantage of .h files is that it contains the definitions only, so as you change the .c file all other .c files don't have to be recompiled. Their simply all linked up. So doing development work is quicker. If you change the .h file all files that include it will obviously have to be recompiled. Its a very efficient way of developing.
 
Old 01-12-2004, 10:57 PM   #9
stoe
LQ Newbie
 
Registered: Jan 2004
Location: ny
Distribution: gentoo
Posts: 8

Rep: Reputation: 0
the reason you experience weird behavior when including a .c file into another .c file is probably due to namespace collisions.

consider this:

Code:
/*in file a.c */
int somefunction(void)
{
     return 0;
}

/* in file main.c */
#include "a.c" 

int main(void)
{
     somefunction();
     return 0;
}
now compiling, you'll see something like:

Code:
$gcc main.c a.c
/tmp/ccJSUHWU.o(.text+0x0): In function `somefunction':
: multiple definition of `somefunction'
/tmp/ccQ4mB6F.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
this is due to the fact you'll now have to functions in the global symbol table with the same name in one executable, so now the compiler can no longer figure out which one to call. as stated in an earlier post, #include simply copies the included vertibim, so in the above example somefunction is defined both in a.c and main.c.
 
Old 01-13-2004, 06:58 AM   #10
ssobeht
Member
 
Registered: Oct 2003
Distribution: Debian Sarge
Posts: 207

Rep: Reputation: 30
cool thread, this is something that is sometimes asumed to be known, but nowbody talked to me about all these differences before (anyway, i use .h and .c files file dvision system ).

biez!
 
Old 01-13-2004, 08:19 AM   #11
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
Most interesting! Like ssobeht I always wanted to know but was afraid to ask
 
Old 01-14-2004, 05:57 AM   #12
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Another very useful property of header files is being able to include libraries. A library is a pre compiled set of functions that can be included again and again in source code. .a for a static library and .so for shared.

When you include the header file stdio.h this includes a list of function, constant and struct definitions which are inserted into your code. When you compile your program gcc will link against the appropriate library of object code and append it to your executable.
 
  


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
need help with organizing contacts in Kopete LaptopLinux Linux - Software 0 10-29-2005 09:39 AM
mp3 organizing doralsoral Linux - Software 1 06-16-2005 04:59 PM
Is this a good way of organizing the code? elmafiacs Programming 3 02-10-2005 06:01 PM
organizing files endezeichen Linux - General 10 04-13-2004 04:21 PM
organizing my files and directories ryancw Linux - Newbie 1 08-30-2003 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration