C Runtime Library, C standard library, Windows API differences and contacts

Source: Internet
Author: User

C Run-Time library functions
C Run-time library functions are some of the basic functions supported by the C language itself, usually implemented directly by the assembler.

API functions
API functions are functions implemented by the operating system to implement specific functions for user-friendly design of applications, and API functions are also implemented in C language functions.

The difference between them is that the API function is for the operating system, and the C language runtime function is for the C language itself.

• 1, run-time library is C Run-time libraries, is C and not the C + + language world concept.
This name is because your C program requires functions in these libraries to run.

• 2, C language is so-called "small kernel" language, its language itself is very small (not many keywords, program flow control, data types, etc.);
So, after the C language kernel was developed, Dennis Ritchie and Brian Kernighan used C itself to rewrite more than 90% of UNIX systems.
function, and separate the most commonly used parts, forming the header file and the corresponding Library,c Run-time library.
Formation of.

• 3, subsequently, with the popularity of C language, each C compiler manufacturer/individual/group follow the old tradition, on different platforms have the corresponding
Standard Library, but most implementations are related to each platform. Because the C compiler has a lot of support and understanding of C
Differences and subtle differences, so there is an ANSI c;ansi C (subjective intent) detailing the specific meanings of each element of C language
and compiler implementation requirements, the introduction of a new method of function declaration, at the same time set the standard library standards. So C run-time
The library is provided by the compiler manufacturer. As for the header and library functions provided by other vendors/individuals, it should be called a third-party C runtime Library
(Third party C runtime libraries).

• 4, C run-time The library contains initialization code, and error handling code (such as divide by zero processing). The program you wrote
There can be no math library, the program runs as usual, but it can't handle complex math operations, but without the C Run-time Library, main ()
Will not be called, and exit () cannot be responded to. Because the C run-time Library contains the most basic and most commonly used procedures for C program operation

• 5, to the C + + world, there is another concept: standard C + + library, which includes the above mentioned C Run-time library
and STL. The reason for the C Run-time Library is obvious, C + + is a superset of C, there is no reason to re-come to a C + + Run-time
Library. VC for C + + to join the standard C + + library mainly includes: LIBCP.LIB, LIBCPMT. LIB and MSVCPRT.LIB.

• 6. In Windows environment, the C Run-time Library provided by VC is divided into Dynamic Runtime library and static runtime library.

Dynamic Run-Time library
The dynamic run-time library mainly includes:
· DLL library file: Msvcrt.dll (or MSVCRTD. DLL for Debug build)
• The corresponding import library file: MSVCRT.LIB (or MSVCRTD. LIB for Debug build)

Static run-Time library
The main files corresponding to the static Runtime Library (release edition) are:
· 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 like printf are in Msvcrt.dll. In fact, when your program runs, a large part of the time is running in these runtimes. When your program (release version) is compiled, the VC automatically links the corresponding runtime library files (LIBC.lib, LIBCMT.lib, or import library MSVCRT.lib) based on your compilation options (single-threaded, multithreaded, or DLL).

The role of the 2.C run-time library

In addition to providing us with the necessary library function calls (such as memcpy, printf, malloc, and so on), the C run-time library provides another of the most important features to add startup functions to your application.

The main function of the C run-time library startup function is to initialize the program, assign the initial value to the global variable, and load the entry function of the user program.

The entry point for a console program that does not adopt a wide character set is mainCRTStartup (void). Let's take this function as an example to analyze what kind of entry program The runtime Library has added to us. This function is defined in CRT0.C, and the following code is compiled and simplified by the author:

void mainCRTStartup (void)
int Mainret;
/* Get WIN32 Full version information */
_osver = GetVersion ();
_winminor = (_osver >> 8) & 0x00FF;
_winmajor = _osver & 0x00FF;
_winver = (_winmajor << 8) + _winminor;
_osver = (_osver >>) & 0x00ffff;

_ioinit (); /* Initialize LOWIO */

/* GET command line information */
_ACMDLN = (char *) getcommandlinea ();

/* Get environmental information */
_aenvptr = (char *) __crtgetenvironmentstringsa ();

_SETARGV (); /* SET command-line arguments */
_SETENVP (); /* Set Environment parameters */

_cinit (); /* C Data initialization: Global variable initialization, right here! */

__initenv = _environ;
Mainret = Main (__ARGC, __ARGV, _environ); /* Call the main function */

Exit (Mainret);

From the above code, the runtime has done some initialization work before invoking the main or WinMain function of the user program. After initialization is complete, the main or WinMain function that we have written is then called. Only in this way can our C-language runtime libraries and applications work properly.

In addition to CRT0.C, the C run-time library contains wcrt0.c, WINCRT0.C, and wwincrt0.c three files to provide an initialization function. WCRT0.C is a wide-set version of CRT0.C, WINCRT0.C contains entry functions for Windows applications, and WWINCRT0.C is a wide-set version of WINCRT0.C.

The run-time library source code for Visual C + + is not installed by default. If you want to view its source code, you will need to reinstall Visual C + + and select the Install Runtime source code option when reloading.

Here is a console program that uses the C run-time library incorrectly:

int main ()
CFile file;
CString str ("I Love You");
File. Open ("file.dat", Cfile::modewrite | Cfile::modecreate);
CATCH (CFileException, E)
#ifdef _DEBUG
AfxDump << "File could not being opened" << e->m_cause << "\ n";

File. Write (str,str. GetLength ());
File. Close ();

We had a link error when we were "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 error occurs because Visual C + + uses a single-threaded static-link library for the console program by default , and the CFile class in MFC has hidden multiple threads. We just need to click the Project->settings->c/c++ menu and options in the visual c++6.0 to modify the compilation options in the Project options.

C Run-level library and C-standard library relationships

C standard library, as the name implies, since it is the standard, is set by the standard organization. is the standard set by the American National Standards Institute (American Nation Standards Institute,ansi) to standardize the C language library. At first, the C-language libraries used by various universities were different, making it difficult to transplant each other, and in this context, the standard was set.

C Runtime Library, which is related to the platform, that is, related to the operating system. It is provided with different C runtime libraries by different development platforms of different operating systems. However, part of the implementation of the C-run library is based on the C standard library, that is, the C run-point library is the various operating system development tools based on their own platform development library, to some extent, it can be said that C runtime Library is a C standard library of an extension library, just add a lot of C standard library does not have platform-related For example, the strcpy function of the C standard library is responsible for copying a string, but due to the lack of control over the size of the target string buffer, it is very likely to cause a buffer overflow (a large number of buffer overflow attacks are caused by this vulnerability); Windows provides a secure string copy function that enables the same functionality, reducing the likelihood of a buffer attack, strcpy_s. These functions are provided in the form of a C run-time library, of course, different operating systems, C runtime libraries may be different, but support for the C standard library is exactly the same, that is, on different operating systems, the use of the same C-standard library functions will inevitably produce consistent results.

Available in the C standard library are:

L standard input and output (stdio.h).

L file operation (stdio.h).

L character operation (Ctype.h).

L string Manipulation (string.h).

L mathematical function (MATH.H).

L Resource Management (stdlib.h).

L format conversion (stdlib.h).

L Time/Date (time.h).

L Assertion (assert.h).

L Constants on various types (Limits.h & float.h).

You can write a program without the math library, the program will run, but can not handle complex mathematical operations, but without the C Run-time Library, main () will not be called, exit () can not be responded to. Because the C Run-time library contains the most basic and most commonly used functions of C program operation.

The following is the relationship between the C runtime library and the C standard library:

A C runtime library roughly includes the following features:

L Start and exit: Includes the entry function and other functions on which the entry function depends.

L Standard functions: The functions of the C language standard library as defined by the C language standard. (C standard library )

L The I/O:I/O function is encapsulated and implemented, see the I/O initialization section in the previous section.

L Heap: The package and implementation of the heap, see the heap initialization section in the previous section.

L Language Implementation: the implementation of some special functions in the language.

Debug: Code to implement debugging functions.

Differences between operating system APIs and C Runtime Library Crt,c standard Libraries

First, the C language is older than Windows, and the start time for the actual standard of C language is earlier than the development time of the Windows (API concept) system. So the Windows system in the development of the time is fully able to use the C language. The most current statements are implemented in C and assembler. So as long as C, it is possible to use the C standard library.

We assume that the implementation of the Windows API consists of the implementation of some of the C standard library functions, which determines that the implementation of this part of the operating system API is implemented by invoking the standard library, then it needs to be published together with the C standard library DLLs used.

The second is that Microsoft's kernel (including API) development is a static link library that uses a strictly relevant C language for a peace platform, so that it does not have to provide DLLs to be developed and distributed. And the inevitable C library is implemented on the basis of the compilation, that is, the C function in the library is (at least a large proportion) in the C syntax of the assembly code.

If you are Microsoft, which one do you choose? Maybe it's both, maybe the latter.

In general, we say that the C Runtime library implies which platform is the C Runtime Library of which development platform?

The implementation of the CRT is based on the Windows API, and the development of WINDOWSAPI is based on the C language, but is not or is not necessarily based on the CRT (or C standard library).

A step further, although the CRT is based on operating system API implementation, but does not mean that all the CRT encapsulated operating system API, such as some user's permissions control, operating system thread creation, etc. are not part of the C Runtime Library, so for these operations we have to directly invoke the operating system API or other libraries.

To summarize, the C standard library is the basic C language library that can be used by any platform. The CRT, in addition to the C standard library to join the scope, but also extends the platform-related interface library, these interfaces to different platforms to invoke different platforms operating system API.

As shown, programs written in the C standard library can be applied to the Windows platform or to the Linux platform, while applications written with the CRT's additional platform-related library functions cannot run across platforms.

and different platform operating system API implementation, is the C standard library, or assembly it, this can have, can not. After all, so many Windows API just found a call to the C standard library, there is. The concept can be understood, as for the Microsoft implementation of the time based on what considerations do not use the C standard library, or use the C standard library has its own considerations. That's the scope of the research inside the operating system.

(go) differences and connections between C run-in libraries, C-standard libraries, Windows APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.