makefile for c++ compilation not running

Viewed 109

I've made a makefile to compile program calc.cpp:

SRCS=calc.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
FLGS=-std=c++17

all: calc clean

calc:
        g++ $(SRCS) $(FLGS) -o $(OBJS)
        ./calc.o

clean:
        rm -f $(OBJS)

However, I'm getting some strange errors:

  • when I make all, it only prints rm -f calc.o
  • if I remove clean section from all, it prints make: Nothing to be done for 'all'.
  • when I make calc, it prints make: 'calc' is up to date.

What is going on here, and why does make calc not compile my code?

1 Answers

This rule:

calc:
         ...

tells make how a target calc can be built and that it has no prerequisites. This means that if the file exists, make considers it up to date (how else, since it doesn't depend on anything)?

So presumably you have a file named calc already existing in your directory, so when you run make calc make thinks it's up to date.

You have to list some prerequisites of calc if you want make to do something more than try to build the target if it doesn't exist.

Related