依赖于共享库子项目的makefile [英] makefile with dependency on a shared library sub project

查看:60
本文介绍了依赖于共享库子项目的makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个makefile.现在,我只想举一个伪示例,以便我可以讨论这个问题并将其简单化...

So I have a makefile. I'll just put a pseudo example for the moment just enough so that I can talk about the issue and keep it simple...

假设我的makefile文件中包含以下内容:

Let's say I have in my makefile something like this:

# Rule to build my test executable - with dependency on the library mytest
build: libmytest.so
    g++ test.cpp -lmytest.so

# Rule to build mytest library
libmytest.so:
    g++ mytestlib.cpp -fPIC   ... etc ...
    cc -fPIC -Wl,-soname,libmytest.so  ... etc...

因此,如果/当我更改文件 mytestlib.cpp 时,我看到由于mylibtest.so已经存在(来自以前的版本),因此我的 build 规则认为它没关系.

So, if/when I change the file mytestlib.cpp, I see that since mylibtest.so already exists (from a previous build) my build rule thinks it has nothing to do.

所以我的问题是,如果仅更改库文件,如何才能建立该库并因此使test.cpp重新链接到新创建的库?

So my question is how can I get the library to build and therefore test.cpp to relink to the newly create library, if I change only the library file?

推荐答案

这正是make的用途:管理依赖项树.只要告诉您您的图书馆取决于其来源即可:

This is exactly what make is made for: manage a dependency tree. Just tell make that your library depends on its sources:

libmytest.so: mytestlib.cpp
    g++ mytestlib.cpp -fPIC   ... etc ...
    cc -fPIC -Wl,-soname,libmytest.so  ... etc...

利用这些额外的信息,make将比较目标的最后修改时间及其先决条件.如果目标丢失或早于其任何先决条件,make将对其进行重建.这是递归的.如果某个先决条件缺失或相对于其先决条件本身已过时,make将首先对其进行重建.

With this extra piece of information make will compare the last modification times of the target and its prerequisites. If the target is missing or older than any of its prerequisites, make will rebuild it. And this is recursive. If a prerequisite is missing or itself out of date with respect to its own prerequisites, make will rebuild it first.

顺便说一句,不是真正的文件名而不是 build 目标,最好直接使用产品的文件名:

By the way, instead of a build target, which is not the name of a real file, it would be better to use directly the file name of the product:

test: libmytest.so
    g++ test.cpp -lmytest.so

这样,如果 test 是最新的(比 libmytest.so 更新),make不会重建它.您将节省时间.

This way, if test is up to date (more recent than libmytest.so) make will not rebuild it. You will save time.

如果您真的想要一个别名,则可以添加一个伪造的目标:

If you really want an alias for this, you can add a phony target:

.PHONY: build

build: test

特殊的 .PHONY 目标的所有先决条件都有不同的对待:使知道它们不是真实的文件名,即使存在名为 build 的文件,也知道它们不是真实的文件名.必须始终进行重建.在这种情况下,make将检查 test 是否存在并且是最新的,如果需要,请重建 test ,然后在此处停止,因为对于 build (它没有配方).因此,您可以将其视为 test 的一种别名.

All prerequisites of the special .PHONY target are treated differently: make knows that they are not real file names, even if a file named build exists, and that they must always be rebuilt. In this case, make will check that test exists and is up to date, rebuild test if needed, and stop here because there is nothing to do for build (it has no recipe). You can thus consider it as a kind of alias for test.

这篇关于依赖于共享库子项目的makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆