I'm experimenting with my first foray into libraries. 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
This runs just fine.
However when I then try to use that object file:
$ gcc test_hello_world.c -Lbin -lunity -o test
/usr/bin/ld: cannot find -lunity
collect2: error: ld returned 1 exit status
Checking what ld is doing
$ 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
So it looks for bin/libunity.so
and bin/libunity.a
, but no bin/libunity.o
... every internet page I've looked at this evening seems to assume that ld should pick up ".o" files just the same as ".so" or ".a" no questions asked (man pages seem to either similarly expect it or don't mention ".o" at all)... but it's not...
Am I misunderstanding something? Missing some argument?
EDIT: I've now run ar rcs bin/libunity.a bin/unity.o
and then the gcc
command from above. This seems to work (there are other errors). But I still thought ld should work with .o files, am I, and half the web, wrong?
This should work:
$ gcc test_hello_world.c bin/libunity.o -o test
Possibly shouldn't even really use the word "library" to describe a *.o
file.
Never heard of lib*.o
files being suitable for lib*.so
/lib*.a
files.
*.o
files are linked directly without any libfoo.o
==> -lfoo
magic.
Or if you want to turn .o
into a .a
, then use $ ar -crs libunity.a libunity.o
, (see also: https://www.howtogeek.com/427086/how-to-use-linuxs-ar-command-to-create-static-libraries/).
A static library is an ar
archive containing one or more object files.