Learning windows programming (4) -- Starting from libc. lib

Source: Internet
Author: User

Space for chaos
Learning windows programming from scratch (4) -- Starting from libc. lib
From the previous article, we have learned the concept of a C Runtime library. This is not a new thing, but it is generally hidden behind the scenes, c/C ++ doesn't seem to talk about it in windows/linux programming. However, it is generally an important part of C/C ++ programming by default. Think about it. Isn't the strcpy, memset, and memcpy provided by the C Runtime Library?

Now, we have to study this thing.

As mentioned earlier, for single-Thread/multi-thread, static/dynamic connections, and whether it is a debug version, the C Runtime Library of VC6 provides six versions. For details, see the following.

 

For each of the selected LIB files, a list is introduced in the previous article, so there is no re-writing here. Not all of them will be studied here at once, or starting from the simplest part according to the principle of simplicity and depth, of course, when other parts are involved, to some extent.

The simplest of course is Single-Threaded, which is also static link. The corresponding file is LIBC. LIB. The CL compilation option is/ML.

Since we want to study this LIB file, it is of course the best to have the source code, jjhou did not say, "there is no secret in front of the source code. Where can we find the source code?

If you have installed VC6, it carries the source code of CRT. The specific directory is related to the directory where you installed VC6. The path on my computer is d: Program FilesMicrosoft Visual StudioVC98CRTSRC ", when you enter, you will find many familiar names, such as "MATH. H "," STDIO. H "," STDLIB. H "," STRING. H ", because the C Runtime Library is included in the C ++ Runtime Library, there is also the code of the C ++ standard library, therefore, we can also see C ++ std header files such as "IOSTREAM", "CSTDIO", and "algorithm.

There is a problem here. There are hundreds of files, and it is impossible to read them all one by one. How can we find the key part?

If you are still impressed with the analysis in the previous article and have problems, you can easily think of hello, which does not contain defaultlibs information in link. the obj file contains two LINK2001 errors, namely, the _ printf and _ mainCRTStartup symbol files cannot be found.

When compiling a C program, the compiler CL uses an underscore (_) to mark the function to be compiled. _ printf refers to the printf function, and _ mainCRTStartup refers to mainCRTStartup, where is it used?

The entry point of a shard ).

Portal

Anyone who has learned C language knows that there is a main function, which is the entry point of a program. In any case, the main function is special. In section 1.1 of TCPL ("The C programming Language" by K & R), let's take a look at what we say during Hello World:

Now, for some explanations about the program itself. a c program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation. C functions are like the subroutines and functions in Fortran or the procedures and functions of Pascal. our example is a function named main. normally you are at liberty to give functions whatever names you like, but "main" is special-your program begins executing at the beginning of main. this means that every program must have a main somewhere.

This passage makes it difficult for many people, including me, to change their thinking. We always think that main is the entry function of the C program. But is that true?

All the children's shoes that use Assembly know that the Assembly entry function is just a symbol and can be defined at will. Then, the assembly code will be executed from the entry and from the PC. There is also a mainCRTStartup (call, many restrictions, good bypassing...) before the main function of callback ......).

Crt0.c

Why do we need mainCRTStartup? Let's take a look at the source code implementation. Search for the file where mainCRTStartup is located. The Code is as follows:

Show sourceview sourceprint? 001 /***

002 * crt0.c-C runtime initialization routine

003 *

004 * Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.

005 *

006 * Purpose:

007 * This the actual startup routine for apps. It callthe users main

008 * routine [w] main () or [w] WinMain after running Ming C Run-Time Library

009 * initialization.

010 *

011 * (With ifdefs, this source file also provides the source code

012 * wcrt0.c, the startup routine for console apps with wide characters,

013 * wincrt0.c, the startup routine for Windows apps, and wwincrt0.c,

014 * the startup routine for Windows apps with wide characters .)

015 *

016 *************************************** ****************************************/

017

018 # ifdef _ WIN32

019

020 # ifndef CRTDLL

021

022 # include <cruntime. h>

023 # include <dos. h>

024 # include <internal. h>

025 # include <stdlib. h>

026 # include <string. h>

027 # include <rterr. h>

028 # include <windows. h>

029 # include <awint. h>

030 # include <tchar. h>

031 # include <dbgint. h>

032

033 /*

034 * wWinMain is not yet defined in winbase. h. When it is, this shoshould be

035 * removed.

036 */

037

038 int

039 WINAPI

040 wWinMain (

041 HINSTANCE hInstance,

042 HINSTANCE hPrevInstance,

043 LPWSTR lpCmdLine,

044 int nShowCmd

045 );

046

047 # ifdef WPRFLAG

048 _ TUCHAR * _ cdecl _ wwincmdln (void );

049 # else/* WPRFLAG */

050 _ TUCHAR * _ cdecl _ wincmdln (void );

051 # endif/* WPRFLAG */

052

053 /*

054 * command line, environment, and a few other globals

055 */

056

057 # ifdef WPRFLAG

058 wchar_t * _ wcmdln;/* points to wide command line */

059 # else/* WPRFLAG */

060 char * _ acmdln;/* points to command line */

061 # endif/* WPRFLAG */

062

063 char * _ aenvptr = NULL;/* points to environment block */

064 wchar_t * _ wenvptr = NULL;/* points to wide environment block */

065

066

067 void (_ cdecl * _ aexit_rtn) (int) = _ exit;/* RT message return procedure */

068

069 static void _ cdecl fast_error_exit (int);/* Error exit via ExitProcess */

070

071 /*

072 * _ error_mode and _ apptype, together, determine how error messages are

073 * written out.

074 */

075 int _ error_mode = _ OUT_TO_DEFAULT;

076 # ifdef _ WINMAIN _

077 int _ app_type = _ GUI_APP; & n

Related Article

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.