0
点赞
收藏
分享

微信扫一扫

makefile静态模式解决多目标问题


静态模式可以更加容易地定义多目标的规则,可以让我们的规则变得更加的有弹性和灵活。格式:

<targets ...> : <target-pattern> : <prereq-patterns ...>
<commands>
...

说明:

  • <targets …>:指定一个或多个目标文件,可使用通配符。
  • <target-pattern>:指定 <targets …>目标文件的模式,如%.o,表示<targets>集合中都是以.o结尾的文件。
  • <prereq-patterns …>:指定<targets …>目标文件依赖的文件的模式,如%.c ,表示 <targets …>集合中的目标文件的依赖文件都是以.c结尾的文件。

<target-pattern> (目标模式)和<prereq-patterns …>(依赖模式)中都要有 % 这个字符,如果文件名中有%可以使用反斜杠 \ 进行转义,来标明真实的 % 字符。
例子

TARGET = main.o hello.o test.o
all:$(TARGET)
$(TARGET):%.o:%.c
gcc -c $< -o $@

“$<”和“$@”是自动化变量,“$<”为依赖目标集和“$@”则是目标集。
上面的$< 从这些main.c hello.c test.c依赖文件集中读取,$@从这些main.o hello.o test.o目标文件集中读取。
运行结果:

~/Desktop/testm$ ls
hello.c main.c Makefile test.c
~/Desktop/testm$ make
gcc -c main.c -o main.o
gcc -c hello.c -o hello.o
gcc -c test.c -o test.o
~/Desktop/testm$ ls
hello.c hello.o main.c main.o Makefile test.c test.o

谢谢阅读!


举报

相关推荐

0 条评论