Link through Header Files

R

rayuthar

Hi,

I am having three files similar to the stucture given below, while i
execute my test.c, it show error /test.c:5: undefined reference to
`myfunction'.
I have included myprogram.h in both the files myprogram.c and test.c.

if i comment out the line myfunction from myfunction.h, then it is
working properly. Is it multiple definition error?
i compile the files as
cc -c myprogram.c -o myprogram
cc -c test.c -o test
cc -o test test.o my

Thanks in Advance!


/******* myprogram.c *******/

#include "myprogram.h"
myfunction()
{
}


/******* myprogram.h *******/

myfunction();


/******** test.c *********/
#include "myprogram.h"
int main()
{
myfunction();
}
 
I

Ian Collins

Hi,

I am having three files similar to the stucture given below, while i
execute my test.c, it show error /test.c:5: undefined reference to
`myfunction'.
I have included myprogram.h in both the files myprogram.c and test.c.

if i comment out the line myfunction from myfunction.h, then it is
working properly. Is it multiple definition error?
i compile the files as
cc -c myprogram.c -o myprogram
cc -c test.c -o test
cc -o test test.o my
Where's myprogram in the line above?
/******* myprogram.h *******/

myfunction();
Don't use implicit it return, please.
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) said: [...]
i compile the files as
cc -c myprogram.c -o myprogram
cc -c test.c -o test
cc -o test test.o my

This can't work, for any reasonable definition of "work".

I suggest you change it, for the time being, to:

cc -c myprogram.c myprogram.o
cc -c test.c -o test.o
cc -o myprogram test.o myprogram.o

You missed the "-o" on the first line.

For that matter, the first two commands can be simplified by using the
default output file name:

cc -c myprogram.c
cc -c test.c

though the "-o" does show more explicitly what's going on.

Note that this is about a particular implementation, not about C.
For more details, consult your system's documentation for the "cc"
command, or ask in comp.unix.programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top