Home  >  Article  >  Operation and Maintenance  >  Win32 SDK Basics (2) Detailed explanation of the cl.exe and link.exe compilation and linking procedures (picture)

Win32 SDK Basics (2) Detailed explanation of the cl.exe and link.exe compilation and linking procedures (picture)

黄舟
黄舟Original
2017-06-06 09:36:593410browse

Start with the compilation process of the program

## We introduced windows in the previous article Knowledge of the classification of programs under the system, compilers, connectors, commonly used header files, library files, etc. This article will talk about the compilation process of the program.

I believe everyone knows that the compilation process of source code is divided into two steps: First, the compilation process, the main job is to translate our source code into intermediate files, which is in windows is the function of cl.exe, it will use our .c file or The .cpp file is translated into an intermediate .obj file; the second is the connection process. The main job is to connect various intermediate files and library files to generate an executable file. This is the role of link.exe in windows, it will .objFiles and library files are linked into exe programs.

This article mainly teaches you to get rid of the IDE of VS and use the command line to use cl.exe and link.exe compile and link the program.

Write our Test.c file

## First We create a new

test.txt file and rename it to text.c. In this file we fill in the following code:

#include "windows.h"
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,  
                     _In_opt_ HINSTANCE hPrevInstance,  
                     _In_ LPWSTR    lpCmdLine,  
                     _In_ int       nCmdShow)   
{
  MessageBox(NULL,"Hello Win32","sdk",MB_ABORTRETRYIGNORE|MB_ICONERROR);
  return 0;
}
    #include "windows.h"

Imports all the necessary header files we need under the

windows system. Then we introduced the main function of the windows window programwWinMain. Here is a brief explanation of the role of each formal parameter in Winmain: hInstance is the instance handle of the current program, which is the memory where the current program is located. The position; hPrevInstanceThe handle of the previous instance of the current program, which has been abandoned; lpCmdLine is the command line parameter, that is, we use the command line When executing the program, you can attach some strings as parameters; nCmdShow is the window display mode, maximize or minimize. We called MessageBox in the main function to define a modal dialog box. If the compilation is successful, a dialog box will pop up after we execute the program.

Now that the

Test.c file has been written, the next thing we have to do is compile and link the files separately.

CompileTest.c

We will open

windows The cmd command line tool, then switch to our current working directory and compile the Test.c file using the following command: Cl.exe /c test.c

After compilation is completed, the test.obj intermediate file will be generated in the working directory:

If your command line prompt cannot find the cl.exe command and other information, I believe you should understand that this is because the environment variable does not exist To import the directory where cl.exe is located, we can first add the environment variable or use the full path to import. If you generate the test.obj file, it means that the compilation process is completed and we can start the connection process.

4. Link generationtest.exe

Next we execute the following command to generate the executable file:

Link.exe test.obj user32.lib

User32.libWe mentioned this library in the previous article. It contains the desired user interface and message-related API. The MessageBox we need to call is defined in this library. , if nothing goes wrong, the executable file test.exe should be generated in your working directory:


## Double-click this exe, and the dialog box we defined pops up:


OK

, the test is over. We completely broke away from VSwith interfaceIDE and generated a custom dialog box. Isn’t it amazing? There should be Xiao Yueyue's mean expression here. . .

The above is the detailed content of Win32 SDK Basics (2) Detailed explanation of the cl.exe and link.exe compilation and linking procedures (picture). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn