explain $@, $<, $1 with examples
时间: 2023-09-07 07:13:34 浏览: 43
In Makefile, the special variables $@, $<, and $1 are used to represent different parts of the rule being executed. Here's what each of them means and how they are used:
1. $@
- $@ represents the target file name. It is used to reference the file name of the target being built. For example:
```
main: main.o utils.o
gcc -o $@ $^
```
In this example, $@ refers to the target file name `main`, and $^ refers to the list of dependencies (`main.o` and `utils.o`).
2. $<
- $< represents the first dependency file name. It is used to reference the first dependency in the list of dependencies. For example:
```
main.o: main.c
gcc -c $<
```
In this example, $< refers to the first dependency file name `main.c`.
3. $1
- $1 represents a variable that is defined by the user. It is used to reference the value of the first argument passed to a makefile target. For example:
```
.PHONY: build
build:
echo "Building $1"
```
In this example, `build` is a target that expects an argument. When called like `make build arg=value`, the value of the argument is referenced using $1.