Building a static library in C using the ar cmd 用ar命令在C中构建一个静态库

huangapple go评论71阅读模式
英文:

Building a static library in C using the ar cmd

问题

I am new to C programming and trying to learn how to build static libraries. I was trying to build an application which suggested me to do the following:

gcc -c testFile.c
ar a testFile.a testFile.o

When I try to execute these cmds, I get an error: "ar: no operation specified"

I am familiar with simple building of a C file, however I don't understand what the second cmd line does and why we need to do this? I understand about .o files, but why do we need to do "ar a"? Is there a simpler way to do the above and avoid the error? Thank you

英文:

I am new to C programming and trying to learn how to build static libraries. I was trying to build an application which suggested me to do the following:

gcc -c testFile.c
ar a testFile.a testFile.o

When I try to execute these cmds, I get an error: "ar: no operation specified"

I am familiar with simple building of a C file, however I don't understand what the second cmd line does and why we need to do this? I understand about .o files, but why do we need to do "ar a"? Is there a simpler way to do the above and avoid the error?Thank you

答案1

得分: 2

根据[Man7]: AR(1)(https://man7.org/linux/man-pages/man1/ar.1.html)(man ar):

r 插入文件 member... 到 archive(带有replacement)中。

对于自定义操作,您可以使用其修饰符(也列在上述URL中)。

示例:

(py_pc064_03.10_test2_pycoralcf) [cfati@cfati-5510-0:/mnt/e/Work/Dev/StackExchange/StackOverflow/q076052480]> ~/sopr.sh
### 将提示设置为较短,以更好地适应粘贴到StackOverflow(或其他)页面中 ###

[064bit提示]> ls
[064bit提示]> # 创建一个目标文件
[064bit提示]> echo "void f() {}" | gcc -x c -c -o dummy.o -
[064bit提示]> ls
dummy.o
[064bit提示]> # 创建(更新)一个存档文件
[064bit提示]> ar rs libdummy.a dummy.o
ar: 创建 libdummy.a
[064bit提示]> ls -Al
总计 8
-rw-r--r-- 1 cfati cfati 1352 Apr 19 12:08 dummy.o
-rw-r--r-- 1 cfati cfati 1490 Apr 19 12:10 libdummy.a
[064bit提示]> # 列出存档成员
[064bit提示]> ar tv libdummy.a
rw-r--r-- 0/0   1352 Jan  1 02:00 1970 dummy.o
英文:

According to [Man7]: AR(1) (man ar):

> r        Insert the files member... into archive (with replacement).

For customizations, you could make use of its modifiers (also listed in the above URL).

Example:

> lang-bash
> (py_pc064_03.10_test2_pycoralcf) [cfati@cfati-5510-0:/mnt/e/Work/Dev/StackExchange/StackOverflow/q076052480]> ~/sopr.sh
> ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
>
> [064bit prompt]> ls
> [064bit prompt]> # Create an object file
> [064bit prompt]> echo "void f() {}" | gcc -x c -c -o dummy.o -
> [064bit prompt]> ls
> dummy.o
> [064bit prompt]> # Create (update) an archive out of it
> [064bit prompt]> ar rs libdummy.a dummy.o
> ar: creating libdummy.a
> [064bit prompt]> ls -Al
> total 8
> -rw-r--r-- 1 cfati cfati 1352 Apr 19 12:08 dummy.o
> -rw-r--r-- 1 cfati cfati 1490 Apr 19 12:10 libdummy.a
> [064bit prompt]> # List archive members
> [064bit prompt]> ar tv libdummy.a
> rw-r--r-- 0/0 1352 Jan 1 02:00 1970 dummy.o
>

huangapple
  • 本文由 发表于 2023年4月19日 16:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定