user5613947
user5613947

Reputation:

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

Upvotes: 0

Views: 135

Answers (2)

user657267
user657267

Reputation: 21000

You haven't specified an include directory for the I option, so during the compilation stage GCC actually interprets -c as an include directory, which cancels the compile-only flag, and then tries to link your program.

You don't need the include flag, you don't even need a recipe for linking because your file layout matches the builtin rules, your entire Makefile (assuming cc is a link to your default compiler) can simply be

log: log.o main.o

Upvotes: 3

NovaDenizen
NovaDenizen

Reputation: 5325

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.

Upvotes: 0

Related Questions