LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 10-07-2005, 04:43 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
Separate Compiling in C++


OK
So I am trying to understand separate compilation and learn how to use 'make' and how to set up Makefiles. The question that I have is regarding a solved example given in a book I am studying from. Here it is:

There are several source files (four). One with the main function, and the rest of them each containing a function used in the main. Now, each of these 'nonmain'
source files HAS ITS OWN HEADER FILE. These header files are then #included in the main source file.
Now, I understand that the book is trying to teach me something, but I am curious if the header files are really necessary (or even usefull) in this case? Can't I just set up a makefile without them, and not mention them in the mainfile?
In what specific case would header files be absolutely essential?

Thanks for your help.
 
Old 10-07-2005, 05:15 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
The header file is essential here. If you do not comile all the source togeterh, for example:

>gcc main.c -c
>gcc nonmain1.c -c
>gcc nonmain2.c -c
>gcc main.o nonmain1.o nonmain2.o -o main

the -c option tells the compile to just make object files (*.o), and not to link them yet. so the functions in nonmain1.c are defined there, but called from main.c. if nonmain1.c and main.c are not compiled together, then main.c will not know what to do with the function defined in nonmain1.c. This is where the header file comes in.

The header file for nonmain1.c (probably named nonmain1.h) is just a DECLARATION of the functions DEFINED in nonmain1.c. (remember that if you DEFINE a function after your main() function, you have to DECLARE it before main(), to let the compiler know that it doesn't need to worry about functions that aren't defined yet, because their definition will be coming along later.). This is what the header file is doing, it is allowing main.c to 'see' the functions that are available in nonmain1.c. that way, when the compiler runs into a function it doesn't know and sees that it has been declared in nonmain1.h, it knows it can move on because the definition will be available later.

by keeping the sources separate, you can save on recompiling time. for example, if you had a directory full of source files named from nonmain1.c to nonmain10000.c, if you compiled the executable all at once, you would have to recompile all the .c files every time you made a change to one. by compiling them separately, you can just recompile the files that have been changed.

make is a tool that can go through your source files, figure out which ones have been changed since the last time you compiled, recompile only the sources that have changed, and then recompile the executable.

hope thats not too long, boring and/or confusing.
 
Old 10-07-2005, 05:17 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
sorry, i forgot you were using c++. same applies, just replace .c with .cpp and gcc with g++.

or...
sed "s/gcc/g++/g" my_text_above | sed "s/\.c /\.cpp /g"
 
Old 10-07-2005, 05:56 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Brief addendum:

1. Yes, separate header files are a Good Thing. For many reasons.
Feel free to inquire further if you'd like to discuss any of these reasons in greater detail.

2. And yes, if you use headers then you very definitely need to include them in your makefile dependencies.

Few bugs are more insidious - and more difficult to reproduce and track down - than data declarations in a
header falling out of sync with the implementation in the body.

'Hope that helps .. PSM
 
Old 10-07-2005, 06:35 PM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
Thanks guys. OK...I guess my follow up question is just another formulation of my original question:
In case when 'nonmain' source file isn't too big and complex, is it possible to combine it with its header file into one file (nonmain.h) that would contain both declaration and definition?
I am probably wrong, but it just seems like a separate header file for a simple
10 line function is an overkill.
 
Old 10-07-2005, 09:36 PM   #6
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
Anybody?
 
Old 10-07-2005, 11:13 PM   #7
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
yes, you could do that, and it probably is overkill. but using a 100 line function to demonstrate the point would also be overkill.
 
Old 10-07-2005, 11:59 PM   #8
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
OK.

Sirclif,
thanks for a good explanation of the purpose of the header file. It makes more sense now.

Last edited by frankie_DJ; 10-08-2005 at 12:01 AM.
 
Old 10-08-2005, 12:21 AM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
frankie_dj - I assume we adequately expressed the following points:

1. You should always use a header file whenever you want to share one or
more declarations between two or more source files.

2. This is vitally true for data definitions (including C++ class definitions);
it's also true (but less urgently so) for function prototypes.

3. You can think of a C/C++ header file as basically an "interface declaration".

4. System headers (like "#include <stdio.h>") use angle brackets.
Local headers (the kind you'd be writing) use double quotes (#include "myheader.h").

5. You set the compiler's search path for include files with the "-I" compiler
switch (equally true for Linux "gcc" as for Windows "cl").

... and, back to the original question ...

6. When you use headers (which you will frequently), you should *always* define
any header dependency in your "Makefile". This is crucial to insure that the
data definition in one module is always in sync with the definition in any other
module that uses the data item!

'Hope that helps .. PSM
 
  


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
separate wallpapers!!!! mrchaos Slackware 11 04-14-2005 01:44 AM
Separate LAN oupajohannes Linux - Networking 1 02-05-2005 10:55 AM
How do I keep the kernels separate? Wolfy Slackware 4 10-22-2004 05:30 AM
Separate Partitions for Separate User groups volvic Slackware - Installation 2 09-16-2004 03:42 AM
how do i separate the partitions...? perry Slackware 2 11-20-2003 04:40 AM


All times are GMT -5. The time now is 01:24 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