Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-06-2004, 04:22 AM   #1
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
include question

I'm currently following "Internetworking with TCP /IP vol III" by Comer, but I'm having trouble compiling his examples, and been forced to change the include / declarations in all his examples. E.g his example.

int some_function_in_seperate_c__file(parameterts);

int current_function() {

return some_function_in_sperate_c_file(p);


I not able to compile this, unless I include the c file

#include "some_function_in...c"

Can this be right?! If so, how come the examples don't how this in the book (I know that it might be a long shot comparing examples, but I think that this is a widely read book). I'm used to c++ , and just trying to get a feel of the C syntax.

Regards Jasper
Old 09-06-2004, 05:47 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Normally, you compile only the .c files not the .h files, so to compile proprely a .c file with a function declared in a .h file, you include the .h file in the .c file.
Old 09-06-2004, 06:35 AM   #3
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Well actually I haven't made matching header files for the function (nor does he do that in the examples). It's a single c file, containing a single function. In the book he only declares the function prototype, but does not include the *.c file. Because of this, I'm not able to compile without including the matching *.c file.
Is it even possible to use a function, with just declaring the function prototype?
Old 09-06-2004, 01:31 PM   #4
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Originally posted by jnusa
I'm not able to compile without including the matching *.c file.
That's not the way to do it, though it may work. #including is for header files, which should only contain:
- Other #includes
- #defines
- struct (or class) definitions
- function prototypes

In other words: things the compiler needs to know, but no code (inline member functions of a C++ class being the exception).

Is it even possible to use a function, with just declaring the function prototype?
Yes, there nothing wrong with that. Sometimes there are good reasons to do that (e.g. functions that call eachother recursively, but which should not be available outside the C-file they're in).

For programs larger than, say, a book example, it is normally recommended to put function prototypes in a header *.h) file, and include it in c-files that need the definitions/prototypes that are in the header.

Now about your main problem:

When you have a program that is split into seperate C-files (.c), you compile them seperately into object-files (.o). The object-files contain the compiled machine-code, but they are not executable: they need to be linked together and some added code/information to become one executable file.

When you have a one-c-file program, and do "gcc -o program program.c", the compiler is smart enough to do both the compiling and linking in one go.

Here's a basic example of a two c-file + one header file program:
/* hello.h */

#define TEXT_TO_PRINT "Hello Denmark!"

void print_hello(void);
/* hello.c */

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

void print_hello(void)
	 printf(TEXT_TO_PRINT "\n");
/* hello_main.c */

/* Note double quotes ( " " ) instead of < > for #including
 * header files that are part of *your* program, as opposed
 * to headers that belong to libraries on the system.

#include "hello.h" 

int main()
	 return 1;
Now, first compile the two c-files into object-files (.o).
(note the -c option to gcc to tell it to compile only and not try to link (which would fail, because hello.c doe not have a main() function):
gcc -Wall -c -o hello_main.o hello_main.c
gcc -Wall -c -o hello.o hello.c
Then do the linking:
gcc -Wall -o hello_program hello.o hello_main.o
Now you can run the program with:
To automate this procedure, makefiles are normally made to provide information what needs to be done. When you have made such a makefile, just typing "make" at the shell-prompt does everything needed. If done properly, make then also knows when not to compile if it is not needed. This is the case when the sources (.h and .c) have not changed since the last object file was created from them.

Make is another subject on its own right..

Last edited by Hko; 09-06-2004 at 01:37 PM.


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
Before you post: What to include in a Hardware question. finegan Linux - Hardware 2 12-20-2007 02:20 AM
Question about /usr/include/bits/typesizes.h stefaandk Linux - General 2 07-21-2005 08:30 PM
php include()/require() script question atom Programming 11 08-19-2004 02:12 AM
question on usr/include/asm/module.h kmack2001 Linux - Newbie 0 02-14-2004 11:40 PM
PHP Question: how to make include(); depending on URL? linuxfond Programming 7 09-05-2003 10:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration