【问题标题】:When using ar command, library not being created使用 ar 命令时,未创建库
【发布时间】:2016-05-02 22:29:42
【问题描述】:

当尝试从 makefile 创建静态库时,该库不会被创建。有人对此有什么意见吗?

all: test.exe

test.exe: test.o
    gcc -o test.exe test.o -L. -ltest

test.o: libtest.a
    gcc -c test.c

libtest.a: ABC-test.o
    ar rcs ABC-test.o

ABC-test.o: A-test.c B-test.c C-test.c
    gcc -c A-test.c B-test.c C-test.c

【问题讨论】:

  • test.o 在之前的编译中是否已经存在?如果确实如此,则 Make 没有理由创建 libtest.a。 test.exe 可能也需要 lib test.a。
  • 以前的编译中不存在。
  • 在编译过程中是否出现错误? ABC-test.o 的规则看起来是错误的,因为它实际上并没有导致 ABC-test.o 被构建。如果不是,请显示构建日志。
  • 如果你在某个时候手工编译过就会这样。

标签: c makefile static-libraries exe


【解决方案1】:

在这条规则中:

libtest.a: ABC-test.o
    ar rcs ABC-test.o

您忘记将库的名称传递给ar。试试这个:

libtest.a: ABC-test.o
    ar rcs libtest.a ABC-test.o

或更好:

libtest.a: ABC-test.o
    ar rcs $@ $^

【讨论】:

  • 还有内置规则:ARFLAGS := rcslibtest.a: libtest.a(ABC-test.o)
猜你喜欢
  • 2020-11-22
  • 1970-01-01
  • 2021-06-20
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
相关资源
最近更新 更多