10

I am using a library which has a sample application. The sample makefile contains $< in the arguments:

all:test.cpp
    g++ -Wl,--no-as-needed -o Example $<
clean:
    rm -f SampleApp11

I've looked this up and tutorialspoint say that

$< the name of the related file that caused the action.

Another website states that:

this is a suffix replacement rule for building .o's from .c's it uses automatic variables $<: the name of the prerequisite of the rule(a .c file) and $@: the name of the target of the rule (a .o file) (see the gnu make manual section about automatic variables) .c.o: $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

I am still confused, what does this means?

Jonas Schäfer
  • 19,041
  • 5
  • 54
  • 69

2 Answers2

17

This is actually nothing to do with the compiler, its part of the Makefile syntax and it is substituted before the compiler is run.

In your example it's the first dependency (file) after the all: target - test.cpp.

The basic function of the Makefile is to create a target if a dependency changes:

target: dependency.cpp
    rule to create target (using dependency.cpp)

Typically $< is the input to the compiler and $@ is the output.

It's sort of as if it was this (not a valid Makefile):

$@: $<
    g++ -o $@ $<

The way I remember them is @ resembles a target (as in target practice) and < resembles an arrow. So I imagine an arrow pointing to a target:

@ <-------- (think "Robin Hood")

YMMV

Galik
  • 44,976
  • 4
  • 80
  • 109
9

It is one of GNU make's automatic variables.

The name of the first prerequisite. If the target got its recipe from an implicit rule, this will be the first prerequisite added by the implicit rule (see Implicit Rules).

A prerequisite is a file listed in the rule:

targets : prerequisites
        recipe

For example, in the following rule the first prerequisite is test.c file:

my_executable: test.c precompiled.o
    g++ -o my_executable $<

It is equivalent to the following:

my_executable: test.c precompiled.o
    g++ -o my_executable test.c

And the precompiled.o is the second prerequisite (a precompiled object file implied).

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
Ruslan Osmanov
  • 19,242
  • 7
  • 43
  • 57