recently, i am learning how to write a makefile on linux.
now, i give my source files and returned mistakes:
f1.h
void f1();
f1.c
1 2 3 4 5
#include "f1.h"
#include <stdio.h>
void f1(){
printf("this is function f1\n");
}
f2.h
void f2();
f2.c
1 2 3 4 5
#include "f2.h"
#include <stdio.h>
void f2(){
printf("this is function f2\n");
}
main.c
1 2 3 4 5 6 7 8 9 10
#include "f1.h"
#include "f2.h"
#include <stdio.h>
int main(){
f1();
f2();
puts("Press any key to continue...");
getchar();
return 0;
}
the follows is my makefile:
makefile
1 2 3 4 5 6 7 8 9 10 11 12
#this is my first makefile
main:main.o f1.o f2.o
gcc -o main main.o f1.o f2.o
main.o:f1.h f2.h
gcc -C main.c
f1.o:f1.h
gcc -C f1.c
f2.o:f2.h
gcc -C f2.c
.PHONY:clean
rm main main.o f1.o f2.o
////////////////////////////////////
when i enter "make" command in terminal, it returns:
gcc -C f1.c
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [f1.o] Error 1
@ne55
think you very much. i have tried as your suggestion, and it works. you have a good eyesight.
@kbw
i kinda new to writing makefile, so i don't understand your writing type. i need to learn more deep. but also think you very much.
The idea is that GNU make knows how to compile and link a C program already, you just need to supply the dependencies.
Lines 1 - 9 should be clear, except $(SRCS:.c=.o). That means take all files that end with .c and replace the extension with .o. It's the list of object files.
Line 11: prog depends on .o files. GNU make knows how to create a .o file from a .c file, and it knows how to create a program from a list of .o files, so you don't need to specify it.
Lines 13-17 should be clear as well, they're header file dependencies.