脚本以在我的工作目录中的所有.c文件中创建一个静态库

发布于 2025-02-10 06:58:31 字数 255 浏览 2 评论 0原文

我正在尝试编写一个脚本,该脚本在工作目录中从所有.c文件中创建静态库调用libwork.a在目录中:

#!/bin/bash
gcc -c *.c | ar cr libwork.a *.o

但是当我运行脚本时,它仅创建对象文件。 libwork.a不会创建。我尝试了采购和执行脚本,但它仍然只创建对象文件。

为什么不创建档案?

I am trying to write a script that create a static library call libwork.a in the working directory from all the .c files in the directory:

#!/bin/bash
gcc -c *.c | ar cr libwork.a *.o

But as I run my script, it only creates the object files. The libwork.a does not get created. I tried both sourcing and executing my script but it still only creates object files only.

Why is it not creating the archive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2025-02-17 06:58:31
  1. 您正在将GCC(最肯定没有)打印的消息输送到AR(该消息没有读取任何内容)。这是无质的,AR应该在GCC之后运行。
  2. “*.o”生成的文件列表已在完成“ GCC”之前传递。

解决方案是卸下该管道,然后简单地将命令一个接一个地运行。

#!/bin/sh -e
gcc -c *.c
ar cr libwork.a *.o

注意“ -e”。这告诉shell如果其中一个命令失败了,那么如果GCC失败,则AR不会执行。

  1. You are piping the messages printed by gcc (most surely none) to ar (which does not read anything). This is nonesense, ar should run after gcc.
  2. The file listing generated by "*.o" is passed before "gcc" finished.

The solution is to remove that pipe and simply run the commands one after the other.

#!/bin/sh -e
gcc -c *.c
ar cr libwork.a *.o

Note the "-e". This tells the shell to abort if one of the commands fails, so if gcc fails ar will not execute.

十二 2025-02-17 06:58:31

另外,看看这个:

#!/bin/bash
gcc -Wall -pedantic -Werror -Wextra -c *.c
ar -rc libwork.a *.o
ranlib libwork.a

Also, have a look at this one:

#!/bin/bash
gcc -Wall -pedantic -Werror -Wextra -c *.c
ar -rc libwork.a *.o
ranlib libwork.a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文