![](/img/trans.png)
[英]How to combine object files (.o) to create static library (.a) for iOS?
[英]How to link static library .o files?
我正在尝试我第一次涉足图书馆。 I am trying to compile the Unity testing framework to a static library using gcc -c -fPIC -std=c99 -Wall -Wextra -pedantic -Werror -Wmissing-declarations -DUNITY_SUPPORT_64 test-framework/unity.c -o bin/libunity.o
这运行得很好。
但是,当我尝试使用该 object 文件时:
$ gcc test_hello_world.c -Lbin -lunity -o test
/usr/bin/ld: cannot find -lunity
collect2: error: ld returned 1 exit status
检查 ld 在做什么
$ ld -Lbin -luinty --verbose
GNU ld (GNU Binutils for Ubuntu) 2.34
Supported emulations:
elf_x86_64
elf32_x86_64
#
# ... script waffle
#
==================================================
ld: mode elf_x86_64
attempt to open bin/libuinty.so failed
attempt to open bin/libuinty.a failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libuinty.so failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libuinty.a failed
attempt to open /lib/x86_64-linux-gnu/libuinty.so failed
attempt to open /lib/x86_64-linux-gnu/libuinty.a failed
attempt to open /usr/lib/x86_64-linux-gnu/libuinty.so failed
attempt to open /usr/lib/x86_64-linux-gnu/libuinty.a failed
attempt to open /usr/lib/x86_64-linux-gnu64/libuinty.so failed
attempt to open /usr/lib/x86_64-linux-gnu64/libuinty.a failed
attempt to open /usr/local/lib64/libuinty.so failed
attempt to open /usr/local/lib64/libuinty.a failed
attempt to open /lib64/libuinty.so failed
attempt to open /lib64/libuinty.a failed
attempt to open /usr/lib64/libuinty.so failed
attempt to open /usr/lib64/libuinty.a failed
attempt to open /usr/local/lib/libuinty.so failed
attempt to open /usr/local/lib/libuinty.a failed
attempt to open /lib/libuinty.so failed
attempt to open /lib/libuinty.a failed
attempt to open /usr/lib/libuinty.so failed
attempt to open /usr/lib/libuinty.a failed
attempt to open /usr/x86_64-linux-gnu/lib64/libuinty.so failed
attempt to open /usr/x86_64-linux-gnu/lib64/libuinty.a failed
attempt to open /usr/x86_64-linux-gnu/lib/libuinty.so failed
attempt to open /usr/x86_64-linux-gnu/lib/libuinty.a failed
ld: cannot find -luinty
所以它寻找bin/libunity.so
和bin/libunity.a
,但没有bin/libunity.o
...我今晚查看的每个互联网页面似乎都假设 ld 应该只获取“.o”文件与 ".so" 或 ".a" 相同 不问任何问题(手册页似乎同样期待它或根本不提及 ".o")......但它不是......
我是不是误会了什么? 缺少一些论点?
编辑:我现在运行ar rcs bin/libunity.a bin/unity.o
然后从上面运行gcc
命令。 这似乎有效(还有其他错误)。 但是我仍然认为 ld 应该与 .o 文件一起使用,对吗,还有一半的 web,错了吗?
这应该有效:
$ gcc test_hello_world.c bin/libunity.o -o test
可能甚至不应该真正使用“库”这个词来描述*.o
文件。 从未听说过lib*.o
文件适用于lib*.so
/ lib*.a
文件。 *.o
文件直接链接,没有任何libfoo.o
==> -lfoo
魔法。
或者,如果您想将.o
变成.a
,则使用$ ar -crs libunity.a libunity.o
,(另请参见: https://www.howtogeek.com/427086/how-to-use-linuxs- ar-command-to-create-static-libraries/ )。 static 库是一个包含一个或多个 object 文件的ar
存档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.