c++ - 生成文件语法 : Static library lib$(library). a($objects)

标签 c++ makefile static-libraries

我正在更新一些 Makefile 以从 Make 3.81 移动到 3.82。在多个地方,原作者使用了类似这样的东西来构建静态库:

all: lib$(library).a($objects)

这似乎依次构建每个 .o 文件并使用 ar 将其插入到 .a 中:

g++ -O2 <snip> -o some_obj.o some_cpp.cpp
ar rv libsome_lib.a some_obj.o
etc...

不过,这个新的 make 版本有以下问题:

*** No rule to make target 'libsome_lib.a()', needed by 'all'

用我习惯的方式替换这个快捷方式是否安全:

lib$(library).a: $(objects)
     ar -rs lib$(library).a $objects

谢谢。

编辑

看来我需要更好的 Makefile 教育。这是原始 Makefile 的较大摘录:

CXXFLAGS += -O2 -g -Wall -Wunused-parameter \
    `pkg-config --cflags gthread-2.0 glibmm-2.4 gtkmm-2.4`

libs +=  `pkg-config --libs gthread-2.0 glibmm-2.4` -lc

%.d: %.cpp
    $(SHELL) -ec '$(CXX) -M $(CPPFLAGS) $(CXXFLAGS) $< \
                      | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
                      [ -s $@ ] || rm -f $@'
%.d: %.c
    $(SHELL) -ec '$(CXX) -M $(CPPFLAGS) $(CXXFLAGS) $< \
                      | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
                      [ -s $@ ] || rm -f $@'

from_sources = $(patsubst %.c,$(2),$(filter %.c, $(1)))  $(patsubst %.cpp,$(2),$(filter %.cpp, $(1)))

sources = $(shell cat sources.inc)
objects = $(call from_sources,$(sources),%.o)
depends = $(call from_sources,$(sources),%.d)

library = some_lib

.PHONY: all clean fresh

all: lib$(library).a($(objects))

clean:
    <SNIP>

if neq($(MAKECMDGOALS),clean)
    include $(depends)
endif

当它在 3.81 下运行时,我创建了所有 .d 依赖项,然后 make 开始 g++ing obj 文件。在 3.82 下,我得到 .d 文件但没有 .o 并且 make 失败并显示“***No rule to make ...”

最佳答案

这是 gnu make 支持的“存档成员”语法。就我的口味而言,它与工具有点太亲密了,但就是这样。最初的错误可能是由于 $(objects) 为空引起的。但我真的不确定。这是一些文档:

http://www.gnu.org/software/make/manual/make.html#Archive-Members

11.1 Archive Members as Targets

An individual member of an archive file can be used as a target or prerequisite in make. You specify the member named member in archive file archive as follows:

 archive(member)

This construct is available only in targets and prerequisites, not in recipes! Most programs that you might use in recipes do not support this syntax and cannot act directly on archive members. Only ar and other programs specifically designed to operate on archives can do so. Therefore, valid recipes to update an archive member target probably must use ar. For example, this rule says to create a member hack.o in archive foolib by copying the file hack.o:

 foolib(hack.o) : hack.o
         ar cr foolib hack.o

In fact, nearly all archive member targets are updated in just this way and there is an implicit rule to do it for you. Please note: The ‘c’ flag to ar is required if the archive file does not already exist.

关于c++ - 生成文件语法 : Static library lib$(library). a($objects),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4457013/

相关文章:

c - 我怎么知道我的 .so 库的足迹?

c++ - C++14 中 char 类型下限的变化是否会破坏与补码系统的兼容性?

c++ - 使用堆上的成员初始化类内的类

c++ - 什么是表示图像矩阵的理想数据结构?

printing - 通过 libcups 以编程方式打印双面打印的正确方法是什么?

c++ - 在遗留非 qmake 构建中处理 Q_OBJECT?

python - 安装最新版本的 Python 时遇到问题 : make error?

c - IAR 使用不同的 #define 构建库

Clang:在哪里定义了 -Wall 选项?

linux - 将 OpenCV 构建为静态库