简体   繁体   English

Mac OS X 上的标准 C 库在哪里?

[英]Where is the standard C library on Mac OS X?

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 are no such files on the system.我试图在 Mac OS X 上找到标准的 C 库。我尝试过类似的路径:“/usr/lib/libc.a”或“/usr/lib/libm.a”,但没有这样的文件系统。 Could you tell me where to find it?你能告诉我在哪里可以找到它吗?

Then I used Terminal at a Linux machine and run such command:然后我在 Linux 机器上使用终端并运行以下命令:

ar t /usr/lib/libc.a

It returns a list of.o files and those.o files are like these:它返回一个 .o 文件列表,这些 .o 文件如下所示:

svc.o
xdr.o
...

What are the meanings of these files?这些文件的含义是什么? where to find them?在哪里可以找到它们?

The standard library is part of libSystem.dylib on OS X.标准库是 OS X 上 libSystem.dylib 的一部分。

It looks like it is:看起来是这样的:

/usr/lib/libSystem.B.dylib

on my machine (MacOS X 10.6.7).在我的机器上(MacOS X 10.6.7)。


You can find out using otool — this is on a Mac running macOS 10.14.2 Mojave, and the (very simple) program was built using Clang from XCode:您可以使用otool进行查找——这是在运行 macOS 10.14.2 Mojave 的 Mac 上,并且(非常简单的)程序是使用 XCode 中的 Clang 构建的:

$ 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:例如,这个河内塔程序是使用自制的 GCC 8.2.0 和ncurses库构建的:

$ 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. otool可以完成许多其他工作——查看手册页。

To answer your second question: static libraries are kept in archive files, hence the .a .要回答您的第二个问题: static 库保存在存档文件中,因此.a As such they are just containers for a bunch of files, just like ZIP, TAR, RAR, etc. minus any compression.因此,它们只是一堆文件的容器,就像 ZIP、TAR、RAR 等减去任何压缩。 Those files listed by the ar (stands for archive ) utility are the original files packed into the archive. ar (代表归档)实用程序列出的那些文件是打包到归档中的原始文件。 You could unpack it and get the original files.您可以将其解压缩并获取原始文件。

Static libraries are in stark contrast to dynamic libraries. Static 库与动态库形成鲜明对比。 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. static 库的内容由 linker 提取并在链接时包含到您的程序中,就好像它们只是程序构建过程的其他编译阶段的结果一样。

Dynamic libraries OTOH 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.动态库 OTOH 不仅是 object 文件的存档,而且它们本身是链接的可执行文件,动态 linker 将它们映射到链接进程地址空间并调整符号表以匹配映射的地址。

Actually it does exist at /usr/lib/system/libsystem_c.dylib .实际上它确实存在于/usr/lib/system/libsystem_c.dylib

You can verify that with: $ nm -gU /usr/lib/system/libsystem_c.dylib您可以使用以下命令进行验证: $ nm -gU /usr/lib/system/libsystem_c.dylib

To answer the other half of your question, OS X does not generally use static libraries ( .a ).要回答您问题的另一半,OS X 通常不使用 static 库( .a )。 As such, there is no libc.a (or libSystem.a ) on OS X.因此,OS X 上没有libc.a (或libSystem.a )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM