208

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."

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.lib Windows' implementation of C standard library functions to work under win32?

jiggunjer
  • 1,905
  • 1
  • 19
  • 18
B.Gen.Jack.O.Neill
  • 8,169
  • 12
  • 51
  • 79

8 Answers8

92

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).

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".

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.

The runtime library is basically a collection of the implementations of those functions in one big file (or a few big files--e.g., on UNIX the floating point functions are traditionally stored separately from the rest). That big file is typically something on the same general order as a zip file, but without any compression, so it's basically just some little files collected together and stored together into one bigger file. The archive will usually contain at least some indexing to make it relatively fast/easy to find and extract the data from the internal files. At least at times, Microsoft has used a library format with an "extended" index the linker can use to find which functions are implemented in which of the sub-files, so it can find and link in the parts it needs faster (but that's purely an optimization, not a requirement).

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.lib and it'll generate a (long) list of the names of all the object files in that library. Those don't always correspond directly to 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 -extract to extract one of those object files, then use dumpbin /symbols <object file name> to find what function(s) is/are in that particular object file.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 63
    You haven't told what "C runtime library" is !! – onmyway133 Apr 01 '14 at 06:19
  • 6
    @entropy: Sure looks to me like I did, but the short answer is that it's a collection of functions, many (but not necessarily all) of which are specified in part 7 of the C standard. – Jerry Coffin Apr 01 '14 at 06:22
  • 6
    This answer implies C libraries are just part of compiler toolchains. Not accurate. – jiggunjer Feb 15 '17 at 10:05
  • 1
    @JerryCoffin Question: so would the `strcpy` function from the C standard library, for example, have it's implementation in the runtime library, or would it just have it's code in normal .c files? – forumulator Dec 28 '17 at 06:45
  • @forumulator: there will normally be source code in a .c file that gets compiled to produce the standard library proper (the ..dll, lib, .a, .so, or whatever happens to apply to the system you're using). – Jerry Coffin Dec 28 '17 at 06:51
  • 3
    So C run time library is basically Microsoft's way of referring to Standard C Library? – ZoomIn Sep 25 '20 at 14:52
  • @Dengey_ikkadnunchi I have the same doubt. – MathGeek Sep 25 '20 at 16:15
  • @JerryCoffin: I think that it would help to mention that while the statically linked libraries go by libc*.lib, the multithreaded dynamically linked library [confusingly] uses a different naming convention and goes by msvcrt.dll, to which there are also many google references. However they are all MS's implementation of the C standard library plus some compiler specific auxiliary functions. – Motorhead Nov 10 '20 at 19:38
  • 3
    @onmyway133 Yes he did. You're suffering from the XY problem. You think you want someone to tell you what a C runtime library is, but when someone does (and quite well and quite clearly, I might add) you still complain, because you really want to ask some completely different question that you probably don't even know how to formulate, probably something like "why would C need a runtime library?" – Marcel Besixdouze Oct 30 '21 at 03:06
  • 2
    So is it possible to run any code at all without a c-runtime library? As in, consider a main.c file like this `int main(void) { volatile int x = 4; volatile int y = x + 5; x *= y; return x;}` can I compile this with `arm-none-eabi-gcc -nostdlib -nostartupfiles -Og -Wl,-emain -mcpu=cortex-a15 main.c -o MyExe.elf` and still run it on suitable Arm Cortex A15 hardware ? I would expect to see R0 set to x and R1 set to y, and then be able to step through and see them update. Is this not the case? – Gregory Fenn Feb 25 '22 at 22:21
  • 2
    @GregoryFenn: Yes, of course it's possible. In fact, for a fair number of small embedded systems, it's pretty routine to write code that doesn't use the standard library at all (though it might use some library code for things like turning LEDs on and off). – Jerry Coffin Feb 25 '22 at 23:30
70

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

see: 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.

fantagons
  • 701
  • 5
  • 2
  • 6
    The highlighted sentence as a way distinguishing from standard library is the first concise accurate answer I've seen not qualified with "most" or "sometimes." – nik.shornikov Mar 17 '17 at 18:16
  • @nik.shornikov: That's because other descriptions try to be more accurate. Although it's true that a standard library is *typically* specific to a compiler and platform, that's not always true. Just for example, at least some versions of both Mingw and Intel's C++ compiler for Windows have used Microsoft's standard library rather than supplying their own. Likewise, Clang on Linux can be (and often is) installed to use the standard library in an existing installation of gcc rather than installing another for itself. – Jerry Coffin Jun 17 '20 at 20:52
  • I'm reasonably certain it's possible to write implementations of both the C and C++ standard libraries that are portable to any platform that supports POSIX. – Jerry Coffin Jun 17 '20 at 20:55
37

I just asked this myself and was hurting my brain for some hours. Still did not find anything that really 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 program starts with the main() function. Other languages might define other functions where the program starts. But a processor does not know the main(). A processor knows only predefined commands, represented by combinations of 0 and 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 ProgramCounter (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 program stack (local variables), as well as the position of the heap (dynamic variables) and the location of global variables (I guess it was called SSA?) within the RAM. A single processor can only execute one program at a time.

That's 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 program is located (the main() function in case of C). And this is what I guess is located within the Runtime Library, and explains why you need a special library for each operating system, cause these are just programs themselves and have different functions to do these things.

This also explains why it is NOT dynamically linked at runtime as .dll files are, even if it is called a RUNTIME Library. The Runtime Library needs to be linked statically, because it is needed at startup of your program. The Runtime Library injects/connects your custom program into/to another program (the operating system) at RUNTIME. This really causes some brain f...

Conclusion: RUNTIME Library is a fail in naming. There might not have been a .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 chosen.

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

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

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
Rumble
  • 660
  • 7
  • 9
  • 4
    I still do not get the ideia. Why do a program needs something at Runtime ? Why the binary code can not run, 100% alone, without anything supporting it at runtime ? Or in other words: Is it possible to have a code that run 100% without anything (including the OS) ? – MarcioAB Jun 04 '17 at 14:29
  • 9
    In theory a program doesn't _need_ a RTL. However, how would your program ever display its result or take any input or request memory without some cooperation from the operating system? – Motorhead Sep 11 '18 at 21:34
  • 3
    This is the most understandable answer for the question, thanks for this. Still the question is does the 'Startup Library' also implement C standard library? As in does the C standard define a specification for library as well like in case of C++ and does this library also implements the same? Please I need enlightenment on this topic. – ZoomIn Sep 25 '20 at 14:59
  • 7
    Your conclusion that the C runtime is needed in order to start the program by providing the address of main() and that the C runtime library is not dynamically linked is wrong. The executable file (such as an ELF executable) contains the entrypoint address, along with other information needed for running the program on an OS. Also, just the name of the Microsoft C Runtime should give you a clue, it's called "msvcrt.dll". – SeanRamey Mar 18 '21 at 00:05
20

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 need to 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.

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".

plinth
  • 48,267
  • 11
  • 78
  • 120
  • 20
    Actually, the C standards describe two types of C environments - "freestanding" and "hosted" - and, in the hosted environments, the functions described in the standards *are* defined as available. In embedded systems, the C environment is typically freestanding, so that you may not have the library routines, or you may be able to avoid using some and use your own replacements. –  Sep 21 '15 at 04:40
10

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.

A good description of the different choices for runtime libraries: 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

Microsoft has a nice list of their runtime library functions:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crt-alphabetical-function-reference?view=msvc-170

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

habakuk
  • 2,712
  • 2
  • 28
  • 47
arinmorf
  • 1,374
  • 16
  • 27
  • 5
    Is struct and enum really a runtime library? – Dean P May 22 '17 at 15:39
  • 1
    @DeanP, no. `struct` and `enum` are typedefs, basically, just syntactic sugar to use basic types like `unsigned int` and `long` and `signed short` in a more logical and structured way. You don't need library code or ant text at all for them as they won't exist in the actual binary executable built! Each enum value will just be a plain number and each struct field will just be populated with raw data -- the "meaning" of struct fields is a source code concept it doesn't exist in the binary executable or object files at all. – Gregory Fenn Feb 25 '22 at 22:29
  • Updated link (right on 2022) to "Microsoft has a nice list of their runtime library functions": [link]https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crt-alphabetical-function-reference?view=msvc-170 and [link]https://learn.microsoft.com/en-us/cpp/c-runtime-library/c-run-time-library-reference?view=msvc-170#:~:text=The%20Microsoft%20runtime%20library%20provides,most%20routines%20in%20the%20library. – andreyk2 Hohlov Mar 18 '22 at 16:36
  • @DeanP nope! the struct and enum is a part of language not library. just look at presented url for complete list. – S.M.Mousavi Aug 06 '23 at 16:51
6

If you use a tool like Dependency Walker on 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.

  • 6
    Only if while compiling that executable, the C runtime was linked dynamically. If it was linked statically, the dependency walker will show nothiing – Eli Bendersky Oct 15 '10 at 14:41
4

i think Microsoft's definition really mean:

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

Andrey
  • 59,039
  • 12
  • 119
  • 163
3

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

* 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.

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

Michael
  • 51
  • 1