简体   繁体   English

Makefile更新了库依赖项

[英]Makefile updated library dependency

I have a large makefile which builds several libraries, installs them, and then keeps on building objects which link against those installed libraries. 我有一个大的makefile,它构建了几个库,安装它们,然后继续构建与这些已安装的库链接的对象。 My trouble is that I want to use "-lfoo -lbar" as g++ flags to link against the two installed libraries, but the dependencies get messed up. 我的麻烦是我想使用“-lfoo -lbar”作为g ++标志来链接两个已安装的库,但依赖关系搞砸了。 If I change a header "42.h" which the library foo depends on, then of course make will rebuild and install it, but it does not appear to notice that my object "marvin" used "-lfoo" and marvin is left linked against the old version... :( 如果我更改库foo所依赖的标题“42.h”,那么make当然会重建并安装它,但它似乎没有注意到我的对象“marvin”使用了“-lfoo”而marvin是左链接的对旧版本...... :(

Thus far, I've been doing: 到目前为止,我一直在做:

$(myObject): $(localSrc) /explicit/path/to/lib/libfoo.a
            $(CXX) $(CPPFLAGS) $(INCFLAGS) -o $@ $^ $(LINKFLAGS) $(LINKLIBS)

But I'm at a point where this is no longer a viable option. 但我现在已经不再是一个可行的选择了。 I need to simply add libraries "-lfoo -lbar" to the LINKFLAGS variable and have the linker figure things out? 我需要简单地将库“-lfoo -lbar”添加到LINKFLAGS变量并让链接器解决问题?

In the mean time, I've aliased a few commands to explicitly blow away the object file(s) in question and then call make, but this is getting silly. 与此同时,我已经使用了一些命令来明确地吹走有问题的目标文件,然后调用make,但这很愚蠢。 I'm pressed for time, but if necessary I could post a small example perhaps Friday evening or Saturday morning. 我很紧张,但如果有必要,我可以在周五晚上或周六早上发布一个小例子。

Hence, I feel like I'm back in some bad version of windows dll hell. 因此,我觉得我回到了一些糟糕的Windows版本地狱。 Is there something I can do to make the linker take notice of the version of the libraries that an object was built against and relink it if those libraries change?? 有什么我可以做的事情让链接器注意到对象构建的库的版本,并重新链接它,如果这些库更改?

Updated: So I hadn't had a chance to crash the suggestions until now. 更新:所以到目前为止我还没有机会崩溃这些建议。 The drawback of what I'm doing is using static libraries. 我正在做的缺点是使用静态库。 So I can't use ldd . 所以我不能用ldd So I rewrote my Makefile and found a way around this problem. 所以我重写了我的Makefile,找到了解决这个问题的方法。 If I get time, I'll post what I did. 如果我有时间,我会发布我所做的。

How about this: 这个怎么样:

LIBS = foo bar blah # and so on

LINKFLAGS = $(addprefix -l,$(LIBS))

LIBPATHS = $(patsubst %,/explicit/path/to/lib/lib%.so, $(LIBS))

$(myObject): $(localSrc) $(LIBPATHS)
        $(CXX) $(CPPFLAGS) $(INCFLAGS) -o $@ $^ $(LINKFLAGS) $(LINKLIBS)

As far as I know, make in general isn't very good at automatically detecting dependencies like this. 据我所知,make一般不会很好地自动检测这样的依赖关系。 (It's not really make's job; make is a higher-level tool that's not aware of the specifics of the commands that it's spawning or what those commands do.) (这不是真正的工作; make是一个更高级别的工具,它不知道它产生的命令的细节或这些命令的作用。)

Two options come to mind. 我想到了两种选择。

First, you could run ldd on $(myObject), save its list of libraries to a text file, then feed that back into your makefile as a list of dependencies. 首先,您可以在$(myObject)上运行ldd ,将其库列表保存到文本文件中,然后将其作为依赖项列表反馈到您的makefile中。 (This is similar to using -MD to save a list of header files to a text file then feeding that back into the makefile as additional rules for source file compilation, as Sam Miller suggested.) (这类似于使用-MD将头文件列表保存到文本文件中,然后将其作为源文件编译的附加规则反馈到makefile中,正如Sam Miller建议的那样。)

Second, you could use a LINKLIBS variable as you've been using, and use GNU Make's functions to let the same variable work for both dependencies and command-line options. 其次,您可以使用LINKLIBS变量,并使用GNU Make的函数让相同的变量适用于依赖项和命令行选项。 For example: 例如:

LINKLIBS := /explicit/path/to/lib/libfoo.so
$(myObject): $(localSrc) $(LINKLIBS)
        $(CXX) $(CPPFLAGS) $(INCFLAGS) -o $@ $^ $(LINKFLAGS) $(patsubst %,-l:%,$(LINKLIBS))

您可以尝试像-MD这样的gcc依赖生成参数,如果您使用它们,我不清楚。

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

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