C语言 什么是 C 运行时库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2766233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:22:05  来源:igfitidea点击:

What is the C runtime library?

cruntime

提问by B.Gen.Hyman.O.Neill

What actually is a C runtime library and what is it used for? I was searching, Googling like a devil, but I couldn't find anything better than Microsoft's: "The Microsoft run-time library provides routines for programming for the Microsoft Windows operating system. These routines automate many common programming tasks that are not provided by the C and C++ languages."

什么是 C 运行时库,它的用途是什么?我一直在搜索,像魔鬼一样在谷歌上搜索,但我找不到比微软更好的东西:“微软运行时库为 Microsoft Windows 操作系统提供了编程例程。这些例程自动化了许多常见的编程任务,这些任务不是由微软提供的。 C 和 C++ 语言。”

OK, I get that, but for example, what is in libcmt.lib? What does it do? I thought that the C standard library was a part of C compiler. So is libcmt.libWindows' implementation of C standard library functions to work under win32?

好的,我明白了,但例如,里面是什么libcmt.lib?它有什么作用?我认为 C 标准库是 C 编译器的一部分。那么libcmt.libWindows 的 C 标准库函数实现是否可以在 win32 下工作?

采纳答案by Jerry Coffin

Yes, libcmt is (one of several) implementations of the C standard library provided with Microsoft's compiler. They provide both "debug" and "release" versions of three basic types of libraries: single-threaded(always statically linked), multi-threaded statically linked, and multi-threaded dynamically linked(though, depending on the compiler version you're using, some of those may not be present).

是的,libcmt 是 Microsoft 编译器提供的 C 标准库的(几个)实现之一。它们提供了三种基本类型库的“调试”和“发布”版本:单线程(始终静态链接)、多线程静态链接多线程动态链接(不过,取决于您使用的编译器版本使用,其中一些可能不存在)。

So, in the name "libcmt", "libc" is the (more or less) traditional name for the C library. The "mt" means "multi-threaded". A "debug" version would have a "d" added to the end, giving "libcmtd".

因此,在名称“libcmt”中,“libc”是(或多或少)C 库的传统名称。“mt”的意思是“多线程”。“调试”版本会在末尾添加一个“d”,给出“libcmtd”。

As far as what functions it includes, the C standard (part 7, if you happen to care) defines a set of functions a conforming (hosted) implementation must supply. Most vendors (including Microsoft) add various other functions themselves (for compatibility, to provide capabilities the standard functions don't address, etc.) In most cases, it will also contain quite a few "internal" functions that are used by the compiler but not normally by the end user.

至于它包含哪些函数,C 标准(第 7 部分,如果您关心的话)定义了一组符合(托管)实现必须提供的函数。大多数供应商(包括 Microsoft)自己添加了各种其他功能(为了兼容性,以提供标准功能无法解决的功能等)在大多数情况下,它还会包含很多编译器使用的“内部”功能但通常不是由最终用户。

If you want to get a complete list of the functions in "libcmt" (to use your example) you could open one of the Visual Studio command prompts (under "Visual Studio Tools", normally), switch to the directory where your libraries were installed, and type something like: lib -list libcmt.liband it'll generate a (long) list of the names of all the object files in that library. Those don't always correspond directlyto the names of the functions, but will generally give an idea. If you want to look at a particular object file, you can use lib -extractto extract one of those object files, then use `dumpbin /symbols " to find what function(s) is/are in that particular object file.

如果您想获得“libcmt”中函数的完整列表(以使用您的示例),您可以打开一个 Visual Studio 命令提示符(通常在“Visual Studio 工具”下),切换到您的库所在的目录安装,然后键入类似的内容:lib -list libcmt.lib它将生成一个()列表,其中包含该库中所有目标文件的名称。这些并不总是直接对应于函数的名称,但通常会给出一个想法。如果你想查看一个特定的目标文件,你可以使用lib -extract提取这些目标文件之一,然后使用 `dumpbin /symbols ' 来查找该特定目标文件中的函数。

回答by fantagons

At first, we should understand what a Runtime Library is; and think what it could mean by "Microsoft C Runtime Library".

首先,我们应该了解什么是运行时库;并思考“Microsoft C 运行时库”可能意味着什么。

see: http://en.wikipedia.org/wiki/Runtime_library

请参阅:http: //en.wikipedia.org/wiki/Runtime_library

I have posted most of the article here because it might get updated.

我在这里发布了大部分文章,因为它可能会更新。

When the source code of a computer program is translated into the respective target language by a compiler, it would cause an extreme enlargement of program code if each command in the program and every call to a built-in function would cause the in-place generation of the complete respective program code in the target language every time. Instead the compiler often uses compiler-specific auxiliary functions in the runtime library that are mostly not accessible to application programmers. Depending on the compiler manufacturer, the runtime library will sometimes also contain the standard library of the respective compiler or be contained in it.

Also some functions that can be performed only (or are more efficient or accurate) at runtime are implemented in the runtime library, e.g. some logic errors, array bounds checking, dynamic type checking, exception handling and possibly debugging functionality. For this reason, some programming bugs are not discovered until the program is tested in a "live" environment with real data, despite sophisticated compile-time checking and pre-release testing. In this case, the end user may encounter a runtime error message.

Usually the runtime library realizes many functions by accessing the operating system. Many programming languages have built-in functions that do not necessarily have to be realized in the compiler, but can be implemented in the runtime library. So the border between runtime library and standard library is up to the compiler manufacturer. Therefore a runtime library is always compiler-specific and platform-specific.

The concept of a runtime library should not be confused with an ordinary program library like that created by an application programmer or delivered by a third party or a dynamic library, meaning a program library linked at run time. For example, the programming language C requires only a minimal runtime library (commonly called crt0) but defines a large standard library (called C standard library) that each implementation has to deliver.

当计算机程序的源代码被编译器翻译成相应的目标语言时,如果程序中的每个命令和对内置函数的每次调用都会导致就地生成,则会导致程序代码的极度膨胀每次使用目标语言编写完整的相应程序代码。相反,编译器通常在运行时库中使用特定于编译器的辅助函数,而这些函数通常是应用程序员无法访问的。根据编译器制造商的不同,运行时库有时也会包含相应编译器的标准库或包含在其中。

还有一些只能在运​​行时执行(或者更有效或更准确)的功能在运行时库中实现,例如一些逻辑错误、数组边界检查、动态类型检查、异常处理和可能的调试功能。因此,尽管进行了复杂的编译时检查和预发布测试,但在使用真实数据在“实时”环境中测试程序之前,不会发现某些编程错误。在这种情况下,最终用户可能会遇到运行时错误消息。

通常运行时库通过访问操作系统来实现很多功能。许多编程语言都有内置函数,这些函数不一定要在编译器中实现,但可以在运行时库中实现。所以运行时库和标准库之间的界限取决于编译器制造商。因此,运行时库总是特定于编译器和特定于平台的。

运行时库的概念不应与应用程序员创建或第三方交付的普通程序库或动态库(即在运行时链接的程序库)混淆。例如,编程语言 C 只需要一个最小的运行时库(通常称为 crt0),但定义了每个实现必须交付的大型标准库(称为 C 标准库)。

回答by Rumble

I just asked this myself and was hurting my brain for some hours. Still did not find anything that realy makes a point. Everybody that does write something to a topic is not able to actually "teach". If you want to teach someone, take the most basic language a person understands, so he does not need to care about other topics when handling a topic. So I came to a conclusion for myself that seems to fit well in all this chaos.

我只是自己问了这个问题,并让我的大脑受伤了几个小时。仍然没有找到任何真正有意义的东西。每个为某个主题写一些东西的人都无法真正“教”。如果你想教一个人,就用一个人理解的最基本的语言,这样他在处理一个话题时就不需要关心其他话题。所以我为自己得出了一个结论,它似乎很适合所有这些混乱。

In the programming language C, every programm starts with the main() function. Other languages might define other functions where the program starts. But a processor does not know main(). A processor knows only predefined commands, represented by combinations of "0" and "1".

在编程语言 C 中,每个程序都以 main() 函数开头。其他语言可能会在程序启动的地方定义其他函数。但是处理器不知道 main()。处理器只知道预定义的命令,由“0”和“1”的组合表示。

In microprocessor programming, not having an underlying operating system (Microsoft Windows, Linux, MacOS,..), you need to tell the processor explicitly where to start by setting the ProgrammCounter (PC) that iterates and jumps (loops, function calls) within the commands known to the processor. You need to know how big the RAM is, you need to set the position of the programm stack (local variables), as well es the position of the heap (dynamic variables) and the location of global variables (i gues it was called SSA?) within the RAM. A single processor can only execute one program at a time.

在没有底层操作系统(Microsoft Windows、Linux、MacOS 等)的微处理器编程中,您需要通过设置在其中进行迭代和跳转(循环、函数调用)的 ProgrammCounter (PC) 来明确告诉处理器从哪里开始处理器已知的命令。你需要知道RAM有多大,你需要设置程序栈的位置(局部变量),以及堆的位置(动态变量)和全局变量的位置(我猜它被称为SSA ?) 在 RAM 中。单个处理器一次只能执行一个程序。

Thats where the operating system comes in. The operating system itself is a program that runs on the processor. A program that allows the execution of custom code. Runs multiple programs at a time by switching between the execution codes of the programs (which are loaded into the RAM). But the operating system IS A PROGRAM, each program is written differently. Simply putting the code of your custom program into RAM will not run it, the operating system does not know it. You need to call functions on the operating system that registers your program, tell the operating system how much memory the program needs, where the entry point into the programm is located (the main() function in case of C). And this is what I gues is located within the RuntimeLibrary, and explains why you need a special library for each operating systems, cause these are just programs themselfs and have different functions to do this things.

这就是操作系统的用武之地。操作系统本身是一个在处理器上运行的程序。允许执行自定义代码的程序。通过在程序的执行代码(加载到 RAM 中)之间切换,一次运行多个程序。但是操作系统是一个程序,每个程序的编写方式都不一样。简单地将自定义程序的代码放入 RAM 不会运行它,操作系统不知道它。您需要调用注册程序的操作系统上的函数,告诉操作系统程序需要多少内存,程序的入口点位于何处(C 中的 main() 函数)。这就是我猜位于 RuntimeLibrary 中的内容,并解释了为什么每个操作系统都需要一个特殊的库,

This also explaines why it is NOT dynamically linked at runtime as .dll files are, even if it is called RUNTIMELibrary. The RuntimeLibrary needs to be linked statically, cause it is needed at startup of your program. The RuntimeLibrary does injected/connected your custom program into/to another program (the operating system) at RUNTIME. This realy causes some brain f...

这也解释了为什么它不像 .dll 文件那样在运行时动态链接,即使它被称为 RUNTIMELibrary。RuntimeLibrary 需要静态链接,因为在程序启动时需要它。RuntimeLibrary 确实在运行时将您的自定义程序注入/连接到/连接到另一个程序(操作系统)。这真的会引起一些大脑...

Conclusion: RUNTIMELibrary is a fail in naming. Might be there have not been .dll (linking at runtime) in the early times and the issue of understanding the difference simply did not exist. But even if this is true, the name is badly choosen.

结论:RUNTIMELibrary 命名失败。早期可能没有 .dll(在运行时链接),并且根本不存在理解差异的问题。但即使这是真的,这个名字的选择也很糟糕。

Better names for the RuntimeLibrary could be: StartupLibrary/OSEntryLibrary/SystemConnectLibrary/OSConnectLibrary

RuntimeLibrary 的更好名称可能是:StartupLibrary/OSEntryLibrary/SystemConnectLibrary/OSConnectLibrary

hope I got it right, up for correction/expansion cheers.

希望我做对了,为更正/扩展欢呼。

回答by plinth

C is a language and in its definition, there do not need to be any functions available to you. No IO, no math routines and so on. By convention, there are a set of routines available to you that you can link into your executable, but you don't needto use them. This is, however, such a common thing to do that most linkers don't ask you to link to the C runtime libraries anymore.

C 是一种语言,在它的定义中,您不需要任何可用的函数。没有 IO,没有数学例程等等。按照惯例,您可以使用一组例程,您可以将它们链接到您的可执行文件中,但您不需要使用它们。然而,这是一件很常见的事情,以至于大多数链接器不再要求您链接到 C 运行时库。

There are times when you don't want them - for example, in working with embedded systems, it might be impractical to have malloc, for example. I used to work on embedding PostScript into printers and we had our own set of runtime libraries that were much happier on embedded systems, so we didn't bother with the "standard".

有时您不想要它们 - 例如,在使用嵌入式系统时,使用 malloc 可能是不切实际的。我曾经致力于将 PostScript 嵌入到打印机中,我们有自己的一组运行时库,它们在嵌入式系统上更快乐,所以我们没有为“标准”而烦恼。

回答by arinmorf

The runtime library is that library that is automatically compiled in for any C program you run. The version of the library you would use depends on your compiler, platform, debugging options, and multithreading options.

运行时库是为您运行的任何 C 程序自动编译的库。您将使用的库版本取决于您的编译器、平台、调试选项和多线程选项。

A good description of the different choices for runtime libraries: http://www.davidlenihan.com/2008/01/choosing_the_correct_cc_runtim.html

运行时库的不同选择的一个很好的描述:http: //www.davidlenihan.com/2008/01/choosing_the_correct_cc_runtim.html

It includes those functions you don't normally think of as needing a library to call:

它包括您通常认为不需要库来调用的那些函数:

  • malloc
  • enum, struct
  • abs, min
  • assert
  • malloc
  • 枚举,结构
  • 腹肌,分钟
  • 断言

Microsoft has a nice list of their runtime library functions:

微软有一个很好的运行时库函数列表:

http://msdn.microsoft.com/en-us/library/2aza74he(VS.71).aspx

http://msdn.microsoft.com/en-us/library/2aza74he(VS.71).aspx

The exact list of functions would vary depending on compiler, so for iOS you would get other functions like dispatch_async() or NSLog().

函数的确切列表因编译器而异,因此对于 iOS,您将获得其他函数,如 dispatch_async() 或 NSLog()。

回答by arinmorf

If you use a tool like Dependency Walkeron an executable compiled from C or C++ , you will see that one of the the DLLs it is dependent on is MSVCRT.DLL. This is the Microsoft C Runtime Library. If you further examine MSVCRT.DLL with DW, you will see that this is where all the functions like printf(), puts(0, gets(), atoi() etc. live.

如果您在从 C 或 C++ 编译的可执行文件上使用Dependency Walker 之类的工具,您将看到它所依赖的 DLL 之一是 MSVCRT.DLL。这是 Microsoft C 运行时库。如果您使用 DW 进一步检查 MSVCRT.DLL,您将看到所有函数(如 printf()、puts(0、gets()、atoi() 等)都存在于此。

回答by Andrey

i think Microsoft's definition really mean:

我认为微软的定义真的意味着:

The Microsoft implementation of standard C run-time library provides...

标准 C 运行时库的 Microsoft 实现提供了...

回答by Michael

There are three forms of the C Run-time library provided with the Win32 SDK:

Win32 SDK 提供了三种形式的 C 运行时库:

* LIBC.LIB is a statically linked library for single-threaded programs.
* LIBCMT.LIB is a statically linked library that supports multithreaded programs.
* CRTDLL.LIB is an import library for CRTDLL.DLL that also supports multithreaded programs. CRTDLL.DLL itself is part of Windows NT. 

Microsoft Visual C++ 32-bit edition contains these three forms as well, however, the CRT in a DLL is named MSVCRT.LIB. The DLL is redistributable. Its name depends on the version of VC++ (ie MSVCRT10.DLL or MSVCRT20.DLL). Note however, that MSVCRT10.DLL is not supported on Win32s, while CRTDLL.LIB is supported on Win32s. MSVCRT20.DLL comes in two versions: one for Windows NT and the other for Win32s.

Microsoft Visual C++ 32 位版本也包含这三种形式,但是,DLL 中的 CRT 被命名为 MSVCRT.LIB。DLL 是可重新分发的。它的名称取决于 VC++ 的版本(即 MSVCRT10.DLL 或 MSVCRT20.DLL)。但是请注意,Win32s 不支持 MSVCRT10.DLL,而 Win32s 支持 CRTDLL.LIB。MSVCRT20.DLL 有两个版本:一个用于 Windows NT,另一个用于 Win32。

see: http://support.microsoft.com/?scid=kb%3Ben-us%3B94248&x=12&y=9

请参阅:http: //support.microsoft.com/?scid=kb%3Ben-us%3B94248&x=12&y=9