I am trying to find the standard C library on Mac OS X. I've tried paths like: "/usr/lib/libc.a" or "/usr/lib/libm.a" , but there aren’t such files on the system. Where can I find it?
Then I used a terminal at a Linux machine and run such a command:
ar t /usr/lib/libc.a
It returns a list of .o files and those .o file开发者_StackOverflow中文版s are like these:
svc.o
xdr.o
...
What are the meanings of these files? Where can I find them?
The standard library is part of libSystem.dylib on OS X.
It looks like it is
/usr/lib/libSystem.B.dylib
on my machine (Mac OS X v10.6.7 (Snow Leopard)).
You can find out using otool
— this is on a Mac running macOS v10.14.2 (Mojave), and the (very simple) program was built using Clang from Xcode:
$ otool -L al
al:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
$ clang --version
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Other programs have more libraries. For example, this Tower of Hanoi program was built with a home-built GCC 8.2.0 and the ncurses
library:
$ otool -L hanoi
hanoi:
/usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
/opt/gcc/v8.2.0/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
And another program uses still more:
$ otool -L $(which sqlcmd)
/Users/jonathanleffler/bin/sqlcmd:
/usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
/usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
isqls09b.dylib (compatibility version 0.0.0, current version 0.0.0)
iasfs09b.dylib (compatibility version 0.0.0, current version 0.0.0)
igens09a.dylib (compatibility version 0.0.0, current version 0.0.0)
iosls09a.dylib (compatibility version 0.0.0, current version 0.0.0)
sobj4/igl4a304.dylib (compatibility version 0.0.0, current version 0.0.0)
sobj4/iglxa304.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/DirectoryService.framework/Versions/A/DirectoryService (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
/opt/gcc/v8.2.0/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
And system programs may use other libraries and frameworks:
$ otool -L $(which passwd)
/usr/bin/passwd:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1561.0.0)
/System/Library/Frameworks/OpenDirectory.framework/Versions/A/OpenDirectory (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libpam.2.dylib (compatibility version 3.0.0, current version 3.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
$ otool -L /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome:
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.63.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 57740.51.2)
/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration (compatibility version 1.0.0, current version 888.51.1)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.50.2)
There are many other jobs that can be done with otool
— look at the man page.
To answer your second question: static libraries are kept in archive files, hence the .a
. As such they are just containers for a bunch of files, just like ZIP, TAR, RAR, etc. minus any compression. Those files listed by the ar (stands for archive) utility are the original files packed into the archive. You could unpack it and get the original files.
Static libraries are in stark contrast to dynamic libraries. A static library's contents are extracted by the linker and included into your program upon linking, as if they were just results of other compilation stages of your program's build process.
Dynamic libraries, on the other hand, are not just archives of object files, but they're linked executables by itself and the dynamic linker maps them into the linking processes address space and adjusts the symbol tables to match the mapped address.
To answer the other half of your question, OS X does not generally use static libraries (.a
). As such, there is no libc.a
(or libSystem.a
) on OS X.
Actually, it did exist at /usr/lib/system/libsystem_c.dylib
.
You could verify that with: nm -gU /usr/lib/system/libsystem_c.dylib
The file has been moved/removed since macOS v11 (Big Sur).
精彩评论