简体   繁体   English

如何在不放置任何物理依赖性的情况下指定Makefile目标构建顺序?

[英]How to specify Makefile target building order without put any physical dependencies?

I have been working on a C++ project that links to a .so(dynamic library) file. 我一直在研究一个链接到.so(动态库)文件的C ++项目。 Let's assume that I have a target PROG which needs to link to a.so, and a.so is also built by me, specified in the following Makefile. 假设我有一个需要链接到a.so的目标PROG,而且我也构建了a.so,在下面的Makefile中指定。

PROG_SRCS = prog.cpp
PROG_OBJS = $(PROG_SRCS: %.cpp:%.o)

all: PROG
PROG: $(PROG_OBJS) liba.so
    $(LINK.cpp) -o $@ $(PROG_OBJS) -la

LIBA_SRCS = liba/a.cpp
LIBA_OBJS = $(LIBA_SRCS: %.cpp:%.o)

liba.so: $(LIBA_OBJS)
    $(LINK.cpp) -shared -Wl.-soname,$@ -o $@ $^

I have used auto-dependency-generation to get the .cpp files get their own dependency on the .h files. 我使用自动依赖生成来使.cpp文件获得它们对.h文件的依赖。 And prog.cpp includes ah 而prog.cpp包括啊

But by this way, once I change a.cpp, liba.so would be remake, then PROG would be remake(relink), which is not what I want. 但通过这种方式,一旦我改变a.cpp,liba.so将被重制,那么PROG将被重制(重新链接),这不是我想要的。 I just change the implementation of liba.so, but not any interface definitions. 我只是改变了liba.so的实现,但没有改变任何接口定义。 PROG should just remake after I change ah PROG应该在我改变后重新制作啊

I want to make that a.so should be built before PROG is built, but the changes of a.so would not incur the building of PROG. 我想在构建PROG之前建立a.so,但a.so的更改不会导致PROG的构建。

The following Makefile is the method I figured out, but with a slightly side-effect (generate a temporary file). 下面的Makefile是我想出的方法,但有一点副作用(生成一个临时文件)。

ORDER = /tmp/.ORDER

all: PROG
PROG: $(PROG_OBJS) $(ORDER)
    $(LINK.cpp) -shared -Wl,-soname,$@ -o $@ $(PROG_OBJS) -la

$(ORDER): liba.so
    test -e $@ || touch $@

In this way, every time liba.so gets remake, $(ORDER) gets remake too. 这样,每次liba.so重制时,$(ORDER)也会被重制。 But it only touch the file if it doesn't exist. 但它只触摸文件,如果它不存在。

Is there any way to specify this kind of dependency without any side-effect, eg creating a tmp file. 有没有办法指定这种依赖,没有任何副作用,例如创建一个tmp文件。

If you're willing to rely on non-portable aspects of GNU make, you can use order-only prerequisites order-only prerequisites for this. 如果您愿意依赖GNU make的非可移植方面,那么您可以使用仅限订单的先决条件 That's the only way to do it other than the stamp file method you've already discovered. 除了你已经发现的戳文件方法之外,这是唯一的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM