Tony Do
Tony Do

Reputation: 11

What is the issue with my makefile?

I am attempting to learn & create my first makefile for my c class assignment and I am receiving an error when I call make saying

gcc: error: main.o: no such file or directory

gcc: error: commands.o: No such file or directory

Makefile:13 recipe for target program failed

make: ***[program] Error 1

This is my makefile:

CC = gcc
CFLAGS = -g -std=c11 -Wall -Wextra -Wpedantic -Werror \
-Wfloat-equal -Wlogical-op -Wshadow -Wswitch-default \
-Wbad-function-cast -Wcast-qual -Waggregate-return \
-fno-diagnostics-show-option $(INCLUDES)

PROG = program
HDRS = commands.h faculty.h
SRCS = main.c commands.c faculty.c
OBJS = $(SRCS:.c=.o)

$(PROG) : $(OBJS)
$(CC) $(OBJS) -o $(PROG)

main.o : main.c commands.h faculty.h

faculty.o : faculty.c faculty.h

commands.o : commands.c commands.h

    rm -f core $(PROG) $(OBJS)

TAGS : $(SRCS) $(HDRS)
    Setags -t $(SRCS) $(HDRS)

Inside my project folder consists of all those headers and c files but when I call the makefile, it does not create the main or the commands objects but it does create the faculty object.

Am I doing something wrong?

Upvotes: 1

Views: 76

Answers (3)

Luis Colorado
Luis Colorado

Reputation: 12635

The line that shows

$(CC) $(OBJS) -o $(PROG)

should be indented with one tab (not spaces, just a tab character) to be correctly parsed.

Your Makefile should read like this to compile correctly your application:

CC = gcc
CFLAGS = -g -std=c11 -Wall -Wextra -Wpedantic -Werror \
-Wfloat-equal -Wlogical-op -Wshadow -Wswitch-default \
-Wbad-function-cast -Wcast-qual -Waggregate-return \
-fno-diagnostics-show-option $(INCLUDES)

PROG = program
HDRS = commands.h faculty.h
SRCS = main.c commands.c faculty.c
OBJS = $(SRCS:.c=.o)

$(PROG) : $(OBJS)
    $(CC) $(OBJS) -o $(PROG)

main.o : main.c commands.h faculty.h

faculty.o : faculty.c faculty.h

commands.o : commands.c commands.h

clean:
    rm -f core $(PROG) $(OBJS)

TAGS : $(SRCS) $(HDRS)
    Setags -t $(SRCS) $(HDRS)

Use tabs (NOT SPACES, this is very important) as indentation chars, and conserve the indentation presented here. To compile, just execute

make

and to clean the project

make clean

Upvotes: 0

Jens
Jens

Reputation: 72609

In addition to Jonathan's answer, if in these two lines

$(PROG) : $(OBJS)
$(CC) $(OBJS) -o $(PROG)

the second line does not start with a tab, you have another problem. Please make sure that cutting & pasting does exactly reproduce what's in your Makefile.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

The problem is that the makefile contains:

commands.o : commands.c commands.h

    rm -f core $(PROG) $(OBJS)

The commands are treated as the 'action' for creating the object file from the source files, overriding the default rule that compiles an object file from a source file.

Consequently, when you try compiling commands.o, the program and all the object files are removed, leading to the complaints from gcc when it tries linking the program.

There are several possible fixes. One is to remove the rm line altogether. Another is to use:

commands.o : commands.c commands.h

clean:
    rm -f core $(PROG) $(OBJS)

Now the rm command is associated with the target clean, and removing the debris like that is a normal operation for a clean target.

Upvotes: 3

Related Questions