makefile - put OBJ files in subdirectory in MAKEFILE
问题描述
my basic Makefile create the .o without the need of writing a line with gcc :
NAME = libtest.h
CC = gcc
CFLAGS = -I. -c
SRCS = example01.c \
example02.c
OBJ = $(SRCS:.c=.o)
all: $(NAME)
$(NAME): $(OBJ)
ar -rc $@ $<
I suppose that the rule $(SRCS:.c=.o)
is making an equivalent as running $(CC) $(CFLAGS) -o $@ $<
because i didn't need to call for the compiler explicitly ?
But then when I try to do the same with the add of a subdirectory in wich goes the OBJ files it doesn't work :
NAME = libtest.h
CC = gcc
CFLAGS = -I. -c
SRCS = example01.c \
example02.c
ODIR = ./builds
OBJ = $(addprefix $(ODIR)/, $(SRCS:.c=.o))
all: $(NAME)
$(NAME): $(OBJ)
ar -rc $@ $<
The subdirectory already exist in this example of course. The error output is : No rule to make target 'builds/example01.o', needed by 'libtest.a'
I saw solutions that both explicitly call the compiler and have the rule $(SRCS:.c=.o)
but i feel like it calls for the compiler two times with no need then ?
解决方案
这个:
我想规则 $(SRCS:.c=.o) 相当于运行 $(CC) $(CFLAGS) -o $@ $<
不太对。如果您不定义自己的规则,Make 有一套内置的模式规则可以应用。其中之一告诉它如何foo.o
从文件构建foo.c
文件。
但是 make 没有内置规则告诉它如何./builds/foo.o
从文件构建foo.c
文件,因此您必须自己提供:
$(ODIR)/%.o : %.c
$(COMPILE.c) -o $@ $<
(这使用了一个内置变量COMPILE.c
,这是内置规则使用的;当然你可以定义你自己的)。
推荐阅读
- python - 带有 Holoviews 的和弦图(很多问题)
- sql - SQL - 列出记录之间的所有页面,同时保持 ID 键
- hadoop - Hadoop fs 和常规 Unix 命令有什么区别?
- apache-spark - Spark forEach 与 Map 函数
- asp.net-core - 如何使用脚本迁移在 ASP.NET Core 2.2 中播种身份角色?
- javascript - 从节点插件多次调用JS函数
- php - 使用 Flask 将 URL 和令牌存储在 cookie 中
- visual-studio-code - 源代码管理功能是否可以选择在模板中包含空格?
- html - 在移动设备上向上滚动时容器会中断
- ruby-on-rails - 将数据集与预定义的数据集匹配并以哈希格式存储