Reputation: 294
I have a C mini-project and I need to create a makefile to build it.
The structure of the src code looks like this
.
├── lib
│ ├── foo.c
│ └── foo.h
├── main.c
└── Makefile
main.c
#include "foo.h"
int main() {
// call a function in another file
myPrintHelloMake();
return 0;
}
foo.c
#include <stdio.h>
#include "foo.h"
void myPrintHelloMake(void) {
printf("Hello makefiles!\n");
return;
}
foo.h
#ifndef _FOO_H_
#define _FOO_H_
void myPrintHelloMake(void);
#endif // _FOO_H_
This is the Makefile I tried to create
CC = gcc
CFLAGS = -I.
RM = rm -f
DEP = ./lib/foo.h
# Object files depend on c file and DEP
%.o: %.c $(DEP)
all : main foo
# Generate output and link object files
foo: ./lib/foo.o
$(CC) ./lib/foo.o -o ./lib/foo
main: main.o ./lib/foo.o
$(CC) main.o ./lib/foo.o -o main
# Generate object files without linking
foo.o: ./lib/foo.c
$(CC) -Wall -c ./lib/foo.c
main.o: main.c ./lib/foo.c
$(CC) -Wall -c main.c ./lib/foo.c
clean:
$(RM) *.o *.out
$(RM) main ./lib/foo
This is the error log
gcc -Wall -c main.c ./lib/foo.c
gcc main.o ./lib/foo.o -o main
gcc ./lib/foo.o -o ./lib/foo
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [foo] Error 1
What I need is
main.c
foo.o
, main.o
) into a specified directory.Thanks for your help.
Upvotes: 1
Views: 311
Reputation: 17513
This Makefile should work:
CC = gcc
CPPFLAGS = -Ilib
CFLAGS = -Wall -O2
RM = rm -f
MAINEXE = main
MAINDEP = lib/foo.h
MAINOBJS = main.o lib/foo.o
all : $(MAINEXE)
# Generate output and link object files
$(MAINEXE): $(MAINOBJS)
$(CC) $(MAINOBJS) -o $@
$(MAINOBJS): $(MAINDEP)
clean:
$(RM) $(MAINEXE) $(MAINOBJS) *.out
.PHONY: all clean
Upvotes: 1