I’m trying to build a program which needs 2 source codes.
Here is the simplified source codes and a makefile.
And I copied some ideas here to write this simple makefile.
But still don’t fully understand why I see this error.
[Source code 1 : main.c]
#include <stdio.h>
#include <stdlib.h>
#include "data.h"
int main(void)
{
test_cfg.a1 = 1;
printf("test_cfg.a1 = %dn", test_cfg.a1);
func();
return 0;
}
[Source code 2 : func.c]
#include <stdio.h>
#include <stdlib.h>
#include "data.h"
int func(void)
{
test_cfg.b1 = 12;
printf("test_cfg.b1 = %dn", test_cfg.b1);
return 0;
}
[Header file : data.h]
#ifndef _DATA_H_
#define _DATA_H_
struct {
int a1;
int b1;
int c1;
} test_cfg;
int func(void);
#endif /* end of _DATA_H_ */
[Makefile]
CC = gcc
AR = ar
ARFLAGS = rcs
CFLAGS += -Wall -std=c99
SOURCE=./main.c ./func.c
OBJECTS=$(SOURCE:.c=.o)
all: app_test
app_test:$(OBJECTS)
$(CC) -o $@ $(OBJECTS)
.c.o:
$(CC) $(CFLAGS) $<
.PHONY: all clean
clean:
rm -f *.o app_test
[Output]
$ make
gcc -Wall -std=c99 main.c
/usr/bin/ld: /tmp/ccjahoYC.o: in function `main':
main.c:(.text+0x2f): undefined reference to `func'
collect2: error: ld returned 1 exit status
make: *** [Makefile:16: main.o] Error 1