WolvenOmega WolvenOmega - 2 months ago 8
C Question

Makefile (with suffixes) in C

So I have three files:

log.c : defines all the functions
log.h : lists all the functions
main.c. : uses the functions


Now, both
log.c
and
main.c
have headers for
log.h
.

gcc log.c main.c


The above runs fine, no errors. However, when I try to make a Makefile following this tutorial, I have:

CC=gcc
CFLAGS=-I

log: main.o log.o
$(CC) -o log main.o log.o -I


When I run
make
, this shows an error every time
main
called a function defined in
log.c
, saying "undefined reference to (function)".

Any help with this would be appreciated.

-bash-4.2$ make
gcc -I -c -o main.o main.c
/tmp/ccQOC7wH.o: In function `main':
main.c:(.text+0x11): undefined reference to `getlog'
main.c:(.text+0x6f): undefined reference to `buildmsg'
main.c:(.text+0x7b): undefined reference to `savelog'
main.c:(.text+0x109): undefined reference to `buildmsg'
main.c:(.text+0x19a): undefined reference to `buildmsg'
main.c:(.text+0x207): undefined reference to `getlog'
main.c:(.text+0x223): undefined reference to `savelog'
main.c:(.text+0x247): undefined reference to `buildmsg'
main.c:(.text+0x24c): undefined reference to `getlog'
main.c:(.text+0x268): undefined reference to `savelog'
main.c:(.text+0x279): undefined reference to `clearlog'
collect2: error: ld returned 1 exit status

Answer

When you ran your makefile that had -I instead of -I., you created an invalid main.o. You need to put the -I. back into CFLAGS and rm *.o from that directory. Then it should make ok.