Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable target in a makefile

Tags:

I am trying to compile set of targets. However it only seems to do the first one. Below is a cut down of the my makefile that shows the error.

  OBJECTS = abc def ghi
  SOURCES = abc.c def.c ghi.c

  $(OBJECTS):     $(SOURCES)
          @echo target is $@, source is $<

In shell,

  $ touch abc.c def.c ghi.c
  $ make

When I run make I get the following output:

  target is abc, source is abc.c

So it only seems to be running the first target.

If I replace $< with $^, the output is:

  target is abc, source is abc.c def.c ghi.c

My question, is it possible to perform expansions on variables like with the (%: %) pattern?

like image 415
wmercer Avatar asked Jun 30 '11 23:06

wmercer


People also ask

What is Target name in makefile?

The target in a makefile rule is usually the name of a file that is to be made as part of the project. This is most commonly an executable file or an object code file. But it doesn't have to be a file (see Phony Targets below). The target must be separated from the prerequisites with a colon.

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.

What is the default target in a makefile?

By default, the goal is the first target in the makefile (not counting targets that start with a period). Therefore, makefiles are usually written so that the first target is for compiling the entire program or programs they describe.


1 Answers

Try this:

  OBJECTS = abc def ghi

  all: $(OBJECTS)

  $(OBJECTS):%:%.c
          @echo target is $@, source is $<

The trouble was

  1. The default target (which is what Make chooses if you just type `make`) is the first target in the makefile, which was `abc`.
  2. You made all sources prerequisites of every object. That is, all three sources were prerequisites of `abc`. They were also prerequisites of `def` and of `ghi`.
like image 180
Beta Avatar answered Oct 19 '22 08:10

Beta