Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with header file creation

Status
Not open for further replies.

hunghsun

Programmer
Aug 25, 2002
31
0
0
US
I am attempting to create header files for my functions but it doesn't work for me. Can someone tell me what I am doing wrong? Below is what I put in each file

hello.h
#include <stdio.h>
void hello(); /* prototype of function */

hello.c
void hello()
{
printf(&quot;Hello World!\n&quot;);
}

test.c
#include &quot;hello.h&quot;
int main(int argc,char *argv[])
{
hello();
return 0;
}

running gcc test.c I got the following error
test.c:4: undefined reference to `hello'
 
You compile with
gcc hello.c test.c

Later on, you will want to use makefiles (man make) which is a tool which optimises the compilations needed to build a program. The idea being that you only recompile hello.c if it actually changes.

--
 
so each time i change something in test.c I need to include hello.c as well? (i.e. rerun using gcc hello.c test.c)

How is that we don't need to include the standard header files when we include it in our program then? For example, why wouldn't I need to recompile stdio.c when I include stdio.h?
 
> so each time i change something in test.c I need to include hello.c as well?
Yes

For small projects, recompiling everything everytime isn't too much of a burden. When you start getting annoyed with long compilation times for every small change, then you need to study the next bit.

On large projects, you would want to do this...
Code:
# hello.c has changed (this produces hello.o)
# only type in this command if hello.c has changed
gcc -c hello.c

# test.c has changed (this produces test.o)
# only type in this command if test.c has changed
gcc -c test.c

# either file has changed, produce the program
# this takes previously compiled modules and produces an executable file
gcc -o myprog test.o hello.o
These commands only compile a program when YOU know that it is necessary to do so. However, this is cumbersome to say the least, which is where make come in. This does all the hard work of figuring out the minimal set of commands needed to recreate a program from the small set of files which you may have changed.

> How is that we don't need to include the standard header files when we include it in our program then?
Because that is part of the standard library - which is called libc. The compiler automatically searches this library each time you compile and link a program.

If you're really curious about what goes on, then try
Code:
gcc -v test.c hello.c

--
 
I will just add a bit to what Salem has said.
If you have 2 files, a.c and b.c make(compile and do not link) them separately using makefiles.

say after
gcc -c a.c -o a.o ( this compiles a.c to produce a.o)
gcc -c b.c -o b.o ( this compiles b.c to produce b.o)

now to link them to produce final executable

gcc -o Out.Exe a.o b.o

Now the problem is that you are using just one command for all the three steps above. Hence it always compiles all the files and then links them.
Rather for std files like stdio.c all the files are precompiled and archieved in libraries like libc.a, libpthread.a etc.
You always compile your .c files and link to the std libraries and create you final exe files.
As you always modify only your own code and not the std C files, hence you dont need to compile them again.

I hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top