ezra
ezra

Reputation: 307

Why is makefile not calling this function?

I am trying to compile all the files with a .c extension into an object folder and then create an executable. However, the Makefile I am using is not registering the source files at all. Instead it is going ahead and calling the command to create the executable with object files that do not exist.

Here is the Makefile:

CC = clang

SOURCE_DIR = src
OBJECT_DIR = objects
EXECUTABLE = main

SOURCES = $(wildcard $(SOURCE_DIR)/*.c)
OBJECTS = $(patsubst $(SOURCE_DIR)/%.c, $(OBJECT_DIR)/%.o, $(SOURCES))

all: $(OBJECTS)
    $(CC) $^ -o $(EXECUTABLE)

clean:
    rm objects/*.o main

$(OBJECT_DIR)/%.o: $(SOURCE_DIR)/%.c
    $(CC) -c $< -o $@

.PHONY: all $(OBJECTS)

And here is the output:

me@hostname:~/parent_dir$ make
clang objects/main.o objects/test.o -o main
clang: error: no such file or directory: 'objects/main.o'
clang: error: no such file or directory: 'objects/test.o'
clang: error: no input files
make: *** [Makefile:11: all] Error 1

And here is the file structure:

parent_dir:
   \ objects (empty):
   \ src:
      - main.c
      - test.c
   - Makefile

Upvotes: 1

Views: 336

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61575

Replace:

.PHONY: all $(OBJECTS)

with:

.PHONY: all

The object files are not phony targets and telling make they are phony tells it that no such files have to exist.

Upvotes: 3

Related Questions