0

我有一个项目,我想从递归转换为非递归 make。结构如下所示

+--app
|  +-- Makefile
+--lib1
|  +-- Makefile
|  +-- x.c
|  +-- y.c
|
+--lib2
|  +-- Makefile
|  +-- x.c

我想要做的是在构建之后具有这样的结构

+--app
|  +-- build/
|  |  +-- debug(or release or test)/
|  |  |  +-- lib1/
|  |  |  |  +-- *.o
|  |  |  |  +-- *.d
|  |  |  +-- lib2/
|  |  |  |  +-- *.o
|  |  |  |  +-- *.d
|  |
|  +-- target/
|  | +-- main.bin
|  |
|  +-- Makefile
|
+--lib1
|  +-- module.mk
|  +-- x.c
|  +-- y.c
|
+--lib2
|  +-- module.mk
|  +-- x.c

主要思想是构建文件夹包含所有对象和依赖文件,并且目标具有应加载的程序文件。

我遇到的问题是 make 永远不会想要创建这个结构。当我定义我的规则时,make 只会运行隐式规则而不是我定义的规则。

我已经阅读了关于非递归制作的所有资源,现在它还没有点击。任何帮助深表感谢。

4

1 回答 1

0

为了使构建结果具有与源相同的目录结构,模式规则必须采用以下形式:

${obj_dir}/%.o : ${src_dir}/%.c

Where % part includes all the subdirectories. The object file must also depend on its directory for make to build the directory first:

.SECONDEXPANSION:
${obj_dir}/%.o : ${src_dir}/%.c | $$(dir $$@)
${obj_dir}/% : mkdir -p $@ 

Depending on the target, one source file can be compiled with/without multi-threading, as position-independent/non-position-independent code, etc.. One way to cope with that is to have separate top-level object file directories (in addition to debug/release top-level directory), e.g.:

${obj_dir}/obj
${obj_dir}/obj-pic
${obj_dir}/obj-mt
${obj_dir}/obj-mt-pic

Once you have these rules working correctly and try a parallel build you will notice that mkdir -p fails when two of them race to create /a/b/a and /a/b/b. The fix is:

${obj_dir}/% : while ! mkdir -p $@; do echo -n ""; done
于 2017-01-11T15:59:16.683 回答