0

I am trying to create a Makefile for a simple project that has 3 files and Makefile. When I compile, console displaysL: make: Nothing to be done for 'all'. And nothing happens

Code:

math: main.o function.o
    g++ main.o function.o -o math

main.o: main.cpp
    g++ -c main.cpp

function.o: function.cpp
    g++ -c function.cpp

clean:
    rm *.o math

Does anyone know the solution to this? Thank you! My IDE is Eclipse FYI

Beta
  • 96,650
  • 16
  • 149
  • 150
RatTaco
  • 5
  • 1
  • That means the targets have a time stamp later than the source. Do a clean first. `make clean` followed by `make`. I don;t see a dependency on header files. If you modify the header file you would want all the source files to be rebuilt (currently they will not). I also don't see an `all` target (that's sort of traditional but not required, but is needed to do a `make all`) – Martin York Dec 14 '20 at 06:40

3 Answers3

1

I suggest adding a line like this one to the end of your Makefile:

all: math

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

My IDE is Eclipse FYI

This is a key detail. You did yourself and others a disservice by burying it in a semi-parenthetical comment at the end of the question.

When I compile, console displaysL: make: Nothing to be done for 'all'.

Your IDE has some specific expectations for makefile conventions, and your makefile does not satisfy them. In particular, the message presented indicates that it is trying to build a target named "all". Your makefile does not provide a rule for building any such target. Although not required in a general sense, it is conventional for makefiles to provide a target named "all" as a top-level entry point. In a makefile that provides this target, building it should build the whole project, with the possible exception of tests and test-support-only targets.

You can resolve this issue by adding the expected target. It can go anywhere with respect to the other rules, but it would be most conventional to put it first. Additionally, it would be a good idea, albeit not required, to declare it .PHONY (see What is the purpose of .PHONY in a makefile?):

all: math

math: main.o function.o
    g++ main.o function.o -o math

main.o: main.cpp
    g++ -c main.cpp

function.o: function.cpp
    g++ -c function.cpp

clean:
    rm *.o math

.PHONY: all
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
-1

i try in my Linux system, it works right.

  1 .PHONY: math
  2 math: main.o function.o
  3     g++ main.o function.o -o math
  4
  5 main.o: main.cpp
  6     g++ -c main.cpp
  7 function.o: function.cpp
  8     g++ -c function.cpp
  9
 10 clean:
 11     rm *.o, math

before the command line, a tab should be exist. enter image description here

yshi
  • 11
  • 3
  • 1
    Thanks for the reply! What is .PHONY for? – RatTaco Dec 14 '20 at 07:54
  • 1
    @RatTaco, `.PHONY` is inappropriate here, and I see no reason to think that its addition would make a difference for you. For more information, see [What is the purpose of .PHONY in a makefile?](https://stackoverflow.com/q/2145590/2402272) – John Bollinger Dec 14 '20 at 13:01