-
Makefiles!
Can't get my head around them!
I have myfunctions.h:
Code:
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
extern int triangularnumber (int number);
#endif
myfunctions.c:
Code:
#include <stdio.h>
int triangularnumber ( int number )
{
int result, i, number_adj;
result = number;
for (i = 1; i < number; i++)
{
number_adj = number - i;
result = result + number_adj;
}
return result;
}
And a program disjointsubset.c:
Which is quite big but has this line:
Code:
if(number_equal >= triangularnumber (no_of_rows_count - 1))
{
//code
}
My makefile is called Makefile:
Code:
CFLAGS=-g
default: disjointsubset
myfunctions.o: myfunctions.c myfunctions.h
disjointsubset.o: disjointsubset.c myfunctions.h
main: disjointsubset.o myfunctions.o
gcc -g -o disjointsubset disjointsubset.o myfunctions.o
clean:
rm -f *.o disjointsubset
backup:
tar cf backup.tar disjointsubset.c myfunctions.h myfunctions.c Makefile
And my error:
(From terminal)
Code:
:>make
cc disjointsubset.o -o disjointsubset
disjointsubset.o(.text+0x9ed): In function `main':
/home/shill/cprogramming/disjointsubset.c:184: undefined reference to `triangularnumber'
collect2: ld returned 1 exit status
make: *** [disjointsubset] Error 1
Not sure what i'm doing wrong as this is about as simple as i can get it!
Can you spot anything i'm doing wrong?
It all worked when the function was declared inside disjointsubset.c!
Thanks
-
Quote:
Code:
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
extern int triangularnumber (int number);
#endif
I don't think you need extern there (it's just a function prototype, it's not supposed to be have the code in this file).
And I'm assuming you've #include'd "myfunctions.h" in your big file.
-
default: disjointsubset
should be
default: main
Or better, rename the main target to be disjointsubset.
-
I suspect you are not including myfunctions.h in your disjointsubset.c. That's what that warning says, anyways.
tabstop is right, you don't need extern for function prototypes [but there's nothing wrong with having the extern there either - it's optional, and it's best if you are consistant - either always or never!]
Also, the "default" target should really be called "all", as that's the common target to use for "make this thing"--
Mats
-
Also (but not causing any problems), you have:
but you never use CFLAGS. Instead you have this:
Code:
gcc -g -o disjointsubset disjointsubset.o myfunctions.o
A better way would be to use your CFLAGS variable (especially once you start writing larger makefiles):
Code:
CFLAGS=-g -Wall -c
LDFLAGS=-o
default: disjointsubset
myfunctions.o: myfunctions.c myfunctions.h
gcc $(CFLAGS) myfunctions.c
disjointsubset.o: disjointsubset.c myfunctions.h
gcc $(CFLAGS) disjointsubset.c
disjointsubset: disjointsubset.o myfunctions.o
gcc $(LDFLAGS) disjointsubset disjointsubset.o myfunctions.o
clean:
rm -f *.o disjointsubset
backup:
tar cf backup.tar disjointsubset.c myfunctions.h myfunctions.c Makefile
Also turn on some warnings (or all warnings with -Wall).
-
You often replace gcc with $(CC) as well.
It should be noted that Make has built-in rules for compiling C files. It knows that a .o file can be generated from a .c file with
Code:
$(CC) $(CFLAGS) -c file.c -o file.o
So cpjust's Makefile is much more concisely written with
Code:
CFLAGS=-g -Wall -c
LDFLAGS=-o
default: disjointsubset
myfunctions.o: myfunctions.h
disjointsubset.o: myfunctions.h
disjointsubset: disjointsubset.o myfunctions.o
gcc $(LDFLAGS) disjointsubset disjointsubset.o myfunctions.o
clean:
rm -f *.o disjointsubset
backup:
tar cf backup.tar disjointsubset.c myfunctions.h myfunctions.c Makefile
Note that, for example, in the rule disjointsubset.o, you don't have to depend on disjointsubset.c, because that's part of the built-in rule.
-
Wow, thanks guys, i hadn't included the header file properly, and i've changed the makefile main to disjointsubset.
Works a treat now.
And i've copied dwks makefile example and understand it (which is always good)!
Thanks again everyone.