views:

48

answers:

1

Hi All,

I'm having a hard time writing makefiles. I have experience in using the extern variables, when I build the project without using makefiles I get absolutely no errors and I can run the program. But from the time I wrote the makefile to build the project, I'm getting undefined reference to errors.

I have more than 3 files with me but, for simplicity's sake I will make use of 3 files here to explain the setup.

/************Project********/
/* main.c */
int x;
main()
{
...

}

/* File1.c*/
extern int x;
fn1()
{
 ...
 }

/* File2.c*/
extern int x;
fn2()
{
 ...
 }
/*******************************/

Upon using the makefile I get undefined reference to errors during the linking time pointing to the File1.c and File2.c?

Am I making any mistake which the eclipse fixes on its own(when makefile is not used) and which surfaces when I use makefile?

My final makefile looks like this -

OBJ1 = algorithm/main.o algorithm/File1.o algorithm/File2.o

all: final

final: main.o algorithm/File1.o algorithm/File2.o
 @echo "Linking - making Final"
 $(CC) -o $@ $(OBJ1)
+2  A: 

There is a discrepancy in your Makefile. Your final target depend on main.o, whereas you give $(OBJ1) to the linker, which includes algorithm/main.o.

Didier Trosset
Ya right, I missed it totally. now I'm able to build. My head had gone fuzzy trying to locate where the problem might be. Thanks a lot.
vikramtheone