The relationship and difference among c Runtime Library, c standard library, and windows API.
Relationship and difference between c Runtime Library, c standard library, and windows API
C Runtime Library Function
C Runtime library functions are some basic functions supported by C language, which are usually directly implemented by assembly.
API functions
API functions are functions provided by the operating system to facilitate the design of applications. API functions are also implemented by C language functions.
Differences
The difference between them is that API functions are for the operating system, while C runtime functions are for the C language itself.
· 1. The Runtime library is the C run-time library, which is a concept of the C language rather than the C ++ language world.
This is because the functions in these libraries are required when your C program runs.
· 2. The C language is a so-called "Small kernel" language, which is small in itself (not many keywords, program flow control, data types, etc );
Therefore, after the C language kernel was developed, Dennis Ritchie and Brian Kernighan used C to rewrite more than 90% of UNIX systems.
Function, and separate the most commonly used parts to form the header file and the corresponding LIBRARY. This is the case for C run-time Library.
Formed.
· 3. Subsequently, with the prevalence of C language, various C compiler manufacturers/individuals/groups follow the old tradition and correspond to each other on different platforms.
But most implementations are related to each platform. Each C compiler has a lot of support and understanding for C.
Differences and subtle differences, so the ansi c; ansi c (subjective intention) specifies the specific meaning of each element of the C Language
And compiler implementation requirements, introduces a new method of function declaration, and sets up the Standard form of Standard Library. So C Runtime
Libraries are provided by compiler manufacturers. Header files and library functions provided by other vendors/individuals/groups should be called third-party C Runtime libraries.
(Third party C runtime libraries ).
· 4. C run-time library contains initialization code and error handling code (for example, pide by zero ). The program you wrote
You can run the program without the math library, but you cannot handle complex mathematical operations. However, if you do not have the C run-time library, main ()
It will not be called, and exit () cannot be responded. Because the C run-time Library contains the most basic and commonly used C program running
Function.
· 5. In the C ++ world, there is another concept: Standard C ++ Library, which includes the above-mentioned C run-time Library
And STL. The reason for including C run-timeLibrary is obvious. C ++ is the superset of C, and there is no reason to re-create a C ++ run-time
Library. The Standard C ++ Library added by VC for C ++ mainly includes LIBCP. LIB, LIBCPMT. LIB, and MSVCPRT. LIB.
· 6. In Windows, the C run-time Library provided by VC is divided into dynamic Runtime Library and static Runtime Library.
Dynamic Runtime Library
The dynamic Runtime Library mainly includes:
· DLL library file: msvcrt. dll (or MSVCRTD. DLL for debug build)
· Corresponding Import Library file: MSVCRT. LIB (or MSVCRTD. LIB for debug build)
Static Runtime Library
The main files corresponding to the static Runtime Library (release version) include:
· LIBC. LIB (Single thread static library, retail version)
· LIBCMT. LIB (Multithread static library, retail version)
Msvcrt. dll provides thousands of C functions. Even low-level functions such as printf are in msvcrt. dll. In fact, most of the time you run your program is in these runtime libraries. When your program (release version) is compiled, VC Automatically releases the corresponding Runtime Library file (libc) based on your compilation options (single thread, multi-thread, or DLL. lib, libcmt. lib or Import Library msvcrt. lib.
2. Role of C Runtime Library
In addition to providing necessary library function calls (such as memcpy, printf, and malloc), the C Runtime Library provides the most important function of adding a startup function for the application.
The main function of the C Runtime Library startup function is to initialize the program, assign the initial value to the global variables, and load the user program's entry function.
The entry point of the console program that does not use the wide character set is mainCRTStartup (void ). Next we will take this function as an example to analyze what kind of Entry Program is added to the Runtime Library for us. This function is defined in crt0.c. The following code has been compiled and simplified by the author:
Void mainCRTStartup (void)
{
Int mainret;
/* Obtain the complete WIN32 version information */
_ Osver = GetVersion ();
_ Winminor = (_ osver> 8) & 0x00FF;
_ Winmajor = _ osver & 0x00FF;
_ Winver = (_ winmajor <8) + _ winminor;
_ Osver = (_ osver> 16) & 0x00FFFF;
_ Ioinit ();/* initialize lowio */
/* Obtain command line information */
_ Acmdln = (char *) GetCommandLineA ();
/* Obtain environment information */
_ Aenvptr = (char *) _ crtGetEnvironmentStringsA ();
_ Setargv ();/* set command line parameters */
_ Setenvp ();/* Set environment parameters */
_ Cinit ();/* C data initialization: Initialize the global variable, right here! */
_ Initenv = _ environ;
Mainret = main (_ argc, _ argv, _ environ);/* call the main function */
Exit (mainret );
}
The code above shows that the Runtime Library performs initialization before calling the main or WinMain functions of the user program. After Initialization is complete, the main or WinMain function we wrote is called. Only in this way can our C Language Runtime libraries and applications work normally.
In addition to crt0.c, the C Runtime Library also contains wcrt0.c, wincrt0.c, and wwincrt0.c files to provide initialization functions. Wcrt0.c is a wide character set version of crt0.c. wincrt0.c contains entry functions of windows applications, while wwincrt0.c is a wide character set version of wincrt0.c.
The source code of the Runtime Library of Visual C ++ is not installed by default. If you want to view its source code, you need to reinstall Visual C ++ and select the option to install the source code of the Runtime Library.
The following shows a console program that uses the C Runtime Library incorrectly:
# Include
# Include
Int main ()
{
CFile file;
CString str ("I love you ");
TRY
{
File. Open ("file. dat", CFile: modeWrite | CFile: modeCreate );
}
CATCH (CFileException, e)
{
# Ifdef _ DEBUG
AfxDump <"File cocould not be opened" <e-> m_cause <"\ n ";
# Endif
}
END_CATCH
File. Write (str, str. GetLength ());
File. Close ();
}
The link error occurred during "rebuild all:
Nafxcwd. lib (thrdcore. obj): error LNK2001: unresolved external symbol _ endthreadex
Nafxcwd. lib (thrdcore. obj): error LNK2001: unresolved external symbol _ beginthreadex
Main.exe: fatal error LNK1120: 2 unresolved externals
Error executing cl.exe.
The cause of the error is that Visual C ++ uses a single-threaded static Link Library for the console program by default, while the CFile class in MFC has hidden multiple threads. In Visual C ++ 6.0, choose Project> Settings> C/C ++ menus and Options, and modify the compilation Options in Project Options.
Relationship between C Runtime Library and C standard library
The C standard library, as its name implies, is developed by the standard organization. It is a standard developed by the American National Standards Institute (ANSI) to regulate the C language library. At the beginning, the C language libraries used by companies in different universities were different, making it very difficult to transplant each other. In this context, this standard was set.
The C Runtime Library is platform-related, that is, operating system-related. It provides different C Runtime libraries for different operating systems and different development platforms. However, part of the implementation of the C Runtime Library is based on the C standard library, that is, the C Runtime Library is the library developed by various development tools of each operating system according to its own platform. To some extent, it can be said that the C Runtime library is an extension library of the C standard library. It only adds platform-related or unrelated library interface functions not available in many C Standard libraries. Example: The strcpy function of the c standard library is responsible for copying strings. However, due to the lack of control over the buffer size of strings, it is very likely to cause Buffer Overflow (a large number of buffer overflow attacks are caused by this vulnerability). On the contrary, Windows provides a safe string copy function that can implement the same function, this reduces the possibility of buffer attacks, strcpy_s. These functions are provided by the c Runtime Library. Of course, the c Runtime Library may be different for different operating systems, but the support for the c standard library is completely consistent, that is, in different operating systems, functions using the same c standard library must produce consistent results.
The C Standard Library provides the following:
L standard input and output (stdio. h ).
L file operations (stdio. h ).
L character operation (ctype. h ).
L string operation (string. h ).
L mathematical functions (math. h ).
L Resource Management (stdlib. h ).
L format conversion (stdlib. h ).
L time/date (time. h ).
L assert. h ).
L constants of various types (limits. h & float. h ).
The program you write can run without the math library, but cannot handle complex mathematical operations. However, without the C run-time library, main () won't be called, exit () cannot be responded. Because the C run-time library contains the most basic and commonly used functions for C program running.
The relationship between the C Runtime Library and the C standard library is as follows:
A c Runtime Library provides the following functions:
L start and exit: including the entry function and other functions on which the entry function depends.
L standard functions: these functions are implemented by the C language standard library. (C Standard Library)
L I/O: encapsulation and Implementation of the I/O function, see the I/O initialization section in the previous section.
L heap: encapsulation and implementation of heap. Refer to heap initialization in the previous section.
L language implementation: implementation of some special functions in the language.
L debugging: code for debugging.
Differences between operating system API and C Runtime Library CRT and C standard library
First, the C language appears earlier than Windows, and the start time of the actual C language standard is earlier than the development time of the Windows (API concept) system. Therefore, the C language can be fully used in Windows development. At present, most of the statements are implemented using C and assembly. If C is used, the C standard library may be used.
We assume that the implementation of Windows API includes the function implementation of some C-standard library functions, this determines that the implementation of this part of the operating system API is implemented by calling the standard library, so you need to add the used c standard library DLL for release at the time of release.
Second, Microsoft's kernel (including API) Development uses a static Link Library of C language that is strictly related to the platform, so that it can be developed and released without providing Dll. Moreover, the C library is implemented on the basis of assembly, that is to say, all the C functions in this library are (at least a large proportion) assembly code with C syntax.
If you want to be Microsoft, which one do you choose? It may be the combination of the two, or the latter.
Generally, the C Runtime Library implies the platform and the C Runtime Library,
The implementation of CRT is based on Windows API, while the development of WindowsAPI is based on C language, but it is not or is not necessarily based on CRT (or C standard library.
Further, although CRT is implemented based on the operating system API, it does not mean that all CRT packages the operating system API, such as permission control of some users, the operating system thread creation does not belong to the C Runtime Library, so we have to directly call the operating system API or other libraries for these operations.
To sum up, the C standard library is the basic C language library that can be used by any platform. In addition to adding the C Standard Library to the scope, CRT also extends the platform-related interface libraries. These interfaces call the operating system APIs of different platforms based on different platforms.
As shown in, programs written in the C standard library can be applied to windows or linux; applications written using CRT and platform-related library functions cannot run across platforms.
But does the operating system API implementation on different platforms use the C standard library or the compilation? This can be done, but it does not. After all, so many windows APIs only need to find one to call the C standard library. After understanding the concept, you can consider the considerations for Microsoft's implementation without using the C standard library or using the C standard library. That is the internal research scope of the operating system. I will confirm this after I know it.