Compile C program from command line

Table of contents

Set MSVC environment variables

C file compilation process

compile

Link


At the beginning of learning, first use the most basic command line tools to learn

Set MSVC environment variables

This is configured under VS2019, the location is related to the location where you installed the IDE

MSVC
D:\VS2019\IDE\VC\Tools\MSVC\14.29.30133

WK10_INCLUDE
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0

WK10_LIB
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0

WK10_BIN
C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0

INCLUDE
%WK10_INCLUDE%\ucrt;%WK10_INCLUDE%\um;%WK10_INCLUDE%\shared;%MSVC%\include;

LIB
%WK10_LIB%\um\x64;%WK10_LIB%\ucrt\x64;%MSVC%\lib\x64;


Path下新增
%MSVC%\bin\HostX64\x64
%WK10_BIN%\x64

Verify environment configuration

 

C file compilation process

illustrate:

  1. C files are preprocessed, macros are replaced, and .i suffix files are generated
  2. Compile, compile into machine code, it is a bunch of binary data, the file suffix is ​​.obj
  3. Link, convert binary machine code into binary language executable program, exe and other executable files

compile

Executing the following command will generate an obj file, which stores the machine code of the c code and the required data support and code support

cl /c Hello.c

This obj file cannot be run directly, but the high-level language is converted into a low-level language, and it can be cross-language, not cross-platform.

The obj file cannot be executed directly. Although it contains machine code, it needs to be agreed with the operating system to execute, such as: code execution location, data execution location, program entry

In addition, the compiler has a strict review mechanism, which can be set, from 1-4, from weak to strong,

cl /c /W(1-4) /P Hello.c

Check the result after replacement, a .i file will be generated, W4 highest inspection standard

cl /c /W4 /P Hello.c

Open Hello.c

This file is the result of replacing include and macros, and it has not been compiled yet.

 

Link

Extract code and data to create an executable program for the current platform

linkCommands are used to link together object files, library files, and other resource files to produce an executable, dynamic-link library (DLL), or static library. You can use linkthe command to Hello.objlink an object file named as an executable.

link Hello.obj

The original obj is only 3kb. After packaging, it is directly made 40 times larger, and an executable file for the Windows platform is generated.

It is found that the winodws platform is actually insensitive to case

 

 

Guess you like

Origin blog.csdn.net/qq_61553520/article/details/131352792