Compiling C++ code from the command line

Links to the original text: https://my.oschina.net/u/160145/blog/264399

Some people haven't written HelloWorld for three months; some people don't know what the compiler is; some people compile the first helloworld, hundreds of ERROR s, lose half of their confidence.
Learn c++, want to test exercises, do not know how to start, you still repeat the new project in the IDE, and then produce a large piece of junk files.
In fact, there is another way of compiling:
That's the legendary Notepad plus command line compilation. As long as you have a U disk, only a computer with windows can write code.

First, you need Platform SDK
WIN7 Windows SDKSMicrosoft Official Download Page
1.GRMSDK_EN_DVD.iso        X86(32bit)
2.GRMSDKIAI_EN_DVD.iso    I64(Itanium)
3.GRMSDKX_EN_DVD.iso     AMD64(64bit)

2. After installation, create a new cmd file under the installed root directory (including Platform SDK, lib, include directory) as follows (take VC8.0 as an example)

@echo off            
@prompt $h   
@title NewVS2005SP1-C/C++MODE    
color f5
 
if "#%vc2005dir_defined%"=="#" (set vc2005dir=%cd%&& set vc2005dir_defined=true)
if "#%path0_defined%"=="#" (set path0=%path% && set path0_defined=true )  

@echo.          
@echo    ******** Welcome to C/C++Compilation mode**********
@echo ┏┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┓ 
@echo ┋        VC 2005 Compiler                      ┋ 
@echo ┋   --------------------------------------   ┋ 
@echo ┋   rc.exe:Resource Hacker                        ┋   
@echo ┋   ml.exe:Macro Assembler Compiler                      ┋ 
@echo ┋   cl.exe:C/C++Compiler                       ┋ 
@echo ┋   link.exe:Linker                          ┋ 
@echo ┋   lib.exe:Lib Gen                         ┋
@echo ┋   nmake.exe:(makefile)Program Maintenance Utility     ┋
@echo ┗┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┛ 
@set PATH=%path0%;%vc2005dir%\bin;%vc2005dir%\PlatformSDK\Bin;%vc2005dir%\myTOOL\windbg\
@set INCLUDE=%vc2005dir%\include;%vc2005dir%\PlatformSDK\Include;%vc2005dir%\atlmfc\include;%vc2005dir%\DirectX_SDK\Include;%vc2005dir%\WTL\Include
@set LIB=%vc2005dir%\lib;%vc2005dir%\PlatformSDK\Lib;%vc2005dir%\atlmfc\lib;%vc2005dir%\DirectX_SDK\Lib\x86;
rem Setting debug symbol paths
@set  _NT_SYMBOL_PATH=%vc2005dir%\myTOOL\windbg\symbols
@echo VC2005 The root directory is%vc2005dir%
@echo (If the above directory is incorrect, environment variables will not be configured correctly path,include,lib)
@echo Please use cd /d Command to source directory,Then start compiling and so on.
echo.                                                  
PROMPT -
if "#%once_defined%"=="#" ( set once_defined=true&&%comspec% /k)


It works like this.

Type CL /? LINK /? Make sure it's correct

3. Whether it's annoying to hit commands every time, there should be another way to generate the target, create a new document Create_VC_UNICODE_v2.0.CMD, and then add the VC path.
if exist D:\software\DevKit\VC set vc2005dir=D:\software\DevKit\VC 


@title VC8.0 Command Line Generation WIN32,UNICODE,RELEASE edition
@rem by wisepragma
@PROMPT -
 
@ECHO  off
@echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rem color 0a
color f6
rem Configure here vc Root directory
if exist D:\Software\VC8.0NEW set vc2005dir=D:\Software\VC8.0NEW
if exist D:\software\DevKit\VC set vc2005dir=D:\software\DevKit\VC

 

@set PATH=%path%;%vc2005dir%\bin;%vc2005dir%\PlatformSDK\Bin;%vc2005dir%\myTOOL\windbg\;%vc2005dir%\myTOOL\leakdiag
@set INCLUDE=%vc2005dir%\include;%vc2005dir%\PlatformSDK\Include;%vc2005dir%\atlmfc\include;%vc2005dir%\DirectX_SDK\Include;%vc2005dir%\WTL\Include
@set LIB=%vc2005dir%\lib;%vc2005dir%\PlatformSDK\Lib;%vc2005dir%\atlmfc\lib;%vc2005dir%\DirectX_SDK\Lib\x86;

rem No spaces or Chinese,Will make mistakes,Garbled


 
rem Take the current directory name as the program name
for %%i in (.) do set outfile=%%~ni
rem set outfile=color
set outdir=.\
set cedir=temp
set cd0=%cd%


@echo Waste is being cleared...
 
>>nul del /f /s /q cl_set.rsp  link_set.rsp  rc_set.rsp *.res *.idb *.pdb *.obj *.exp  *.exe.embed.manifest *.exe.intermediate.manifest *.ncb *.user *.pch *.ilk *.dep  BuildLog.htm *.rc2
 


taskkill.exe /im %outfile%.exe
del /f /s /q %outfile%.exe

md "%outdir%"

@echo ++++++++++Compiling and linking+++++++++++++++++++++++++++++++++++++
@echo Compiling resources...
for  %%x in (*.rc) DO rc /v  %%x


@echo Compiling source files...
rem  /D "_ATL_MIN_CRT" Will produce ERROR:LNK2005 XXX Already in atlmincrt.lib(atlinit.obj) Definition in
>cl_set.rsp		echo /O1 /FD /MT /D "NDEBUG"  /D "STRICT" /D "WIN32" /D "_WINDOWS"   /D "_UNICODE" /D "UNICODE"  /EHsc  /fp:fast /GR-  /W3 /c  /TP   /Os /Fo"%outdir%/" 
>>cl_set.rsp	dir /b *.cpp
cl @cl_set.rsp  /W3 /nologo 

@echo Links in progress... 
>link_set.rsp		echo  "/OUT:%outfile%.exe" /INCREMENTAL:NO /MANIFEST:NO /NODEFAULTLIB:"oldnames.lib"  /STACK:65536,4096 /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
@rem /SUBSYSTEM:WINDOWS 
>>link_set.rsp		dir /b *.res *.obj 
link @link_set.rsp  /nologo   /ERRORREPORT:PROMPT
@REM judge
 if exist %outfile%.exe goto goahead

 :ABORT
rem COLOR 0c
rem color f0

color f3
cd %cd0%

%comspec% /k
:goahead
echo Clean up intermediate garbage... 
del *.res *.obj *.rsp
@del /f /s /q *.aps  *.idb *.pdb *.exp  *.exe.embed.manifest *.exe.intermediate.manifest *.ncb *.user *.pch *.ilk *.dep  BuildLog.htm *.rc2
echo ------Ready to run--------
@echo on
%outfile%.exe
@rem start %outfile%.exe
@ECHO.
@%comspec% /k


So we can compile helloworld.cpp

#include <iostream>
using namespace std;
int main()
{
	cout<<"hellowrold"<<endl;
}
Put the source file in a new directory, copy Create_VC_UNICODE_v2.0.CMD into it, and double-click CMD to generate automatically.




Another example code

//Range of values for C++ numeric types
#include "stdio.h"
#include "iostream"
using namespace std;
int main()
{

char a1=-128;//-128~+127
short b1=-32768;// -32768~+32767
int c1=-2147483648;//-2147483648~+2147483647
long d1=-2147483648;//-2147483648~+2147483647
long long x1=-9223372036854775808;//-9223372036854775808~9223372036854775807
//Take -1 below and convert it to the maximum value.
unsigned char a2=-1;//0~255
unsigned short b2=-1;// 0~65535
unsigned int c2=-1;//0~4294967295
unsigned long d2=-1;//0~4294967295
unsigned long long x2=-1;//0~18446744073709551615
float e=100/3.0;//10e+38,6-digit significant digit-7
double f=100.0/3;//10e+308,12-bit significant digits-16
long double g=100.0/3;//10 + 4932, 15-digit significant digit-16
cout.precision(50);
cout<<"Test platform:Intel P4,XPsp3-32bit,VC8.0"<<endl;
cout<<"Floating point accuracy test 100 divided by 3"<<endl;
cout<<"float \t\t e="<<e<<endl;
cout<<"double \t\t f="<<f<<endl;
cout<<"long double \t g="<<g<<endl;
cout<<"The minimum values of signed numbers are"<<endl;
printf("char a1   ->%d\n",a1);
cout<<"short b1 ="<<b1<<endl;
cout<<"int c1   ="<<c1<<endl;
cout<<"long d1   ="<<d1<<endl;
cout<<"long long x1 ="<<x1<<endl; 
cout<<"....Minimum value minus one more,Maximum spillover"<<endl;
a1--;b1--;c1--;d1--;x1--;
printf("char a1   ->%d\n",a1);
cout<<"short b1 ="<<b1<<endl;
cout<<"int c1   ="<<c1<<endl;
cout<<"long d1   ="<<d1<<endl;
cout<<"long long x1 ="<<x1<<endl; 
cout<<"Maximum unsigned number(-1)Namely"<<endl;
printf("unsigned char a2 ->%d\n",a2);
cout<<"unsigned short b2 ="<<b2<<endl;
cout<<"unsigned int c2   ="<<c2<<endl;
cout<<"unsigned long d2 ="<<d2<<endl;
cout<<"unsigned long long x2 ="<<x2<<endl;
cout<<"...Maximum value,After adding one,overflow,Zeroing(minimum value)"<<endl;
a2++;b2++;c2++;d2++;x2++;
printf("unsigned char a2 ->%d\n",a2);
cout<<"unsigned short b2 ="<<b2<<endl;
cout<<"unsigned int c2   ="<<c2<<endl;
cout<<"unsigned long d2 ="<<d2<<endl;
cout<<"unsigned long long x2 ="<<x2<<endl;
cout<<"....After zero reduction,Overflow again,Maximum value,So don't take countless numbers and less than zero."<<endl;
a2--;b2--;c2--;d2--;x2--;
printf("unsigned char a2 ->%d\n",a2);
cout<<"unsigned short b2 ="<<b2<<endl;
cout<<"unsigned int c2   ="<<c2<<endl;
cout<<"unsigned long d2 ="<<d2<<endl;
cout<<"unsigned long long x2 ="<<x2<<endl;
cout<<"Storage size"<<endl;
cout<<"sizeof(char)   ="<<sizeof(char)<<"byte" <<endl;
cout<<"sizeof(short)   ="<<sizeof(short) <<"byte" <<endl;
cout<<"sizeof(int)   ="<<sizeof(int)<<"byte" <<endl;
cout<<"sizeof(long)   ="<<sizeof(long )<<"byte" <<endl;
cout<<"sizeof(long long) ="<<sizeof(long long)<<"byte" <<endl<<endl;
cout<<"sizeof(unsigned char)   ="<<sizeof(unsigned char)<<"byte" <<endl;
cout<<"sizeof(unsigned short)   ="<<sizeof(unsigned short )<<"byte" <<endl;
cout<<"sizeof(unsigned int)   ="<<sizeof(unsigned int )<<"byte" <<endl;
cout<<"sizeof(unsigned long)   ="<<sizeof(unsigned long )<<"byte" <<endl; 
cout<<"sizeof(unsigned long long) ="<<sizeof(unsigned long long)<<"byte" <<endl<<endl;
cout<<"sizeof(float)   ="<<sizeof(float )<<"byte" <<endl;
cout<<"sizeof(double)   ="<<sizeof(double)<<"byte" <<endl; 
cout<<"sizeof(long double) ="<<sizeof(long double )<<"byte" <<endl;
//Other methods (# include "limits"// other methods
/*
      cout<<"---------The following maxima - ----------------------------""endl";   
      cout<<   numeric_limits<unsigned long long >::max()   <<endl;     
      cout<<   numeric_limits< long long >::max()   <<endl;     
      cout<<"---------The following minimum -------------------------------- "___________ endl;   
      cout<<   numeric_limits<unsigned long long >::min()   <<endl;     
      cout<<   numeric_limits<long long>::min()   <<endl;     
*/
//long long value - 2E (sizeof (long) * 8) / 2 ~ 2E (sizeof (long) * 8) - 1
//The unsigned long length is 0~2E (sizeof (unsigned long)*8) -1
return 0;
}

Operation result
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Waste is being cleared.
H: helloworld New folder cl_set.rsp could not be found
 Error: No process "New Folder. Ex" was found.
Delete files - H: helloworld New folder New folder. exe
 Subdirectory or file.  already exists.
++++++++++Compiling and linking+++++++++++++++++++++++++++++++++++++
Compiling resources...
The source file is being compiled.
howwide.cpp
 Howwide. CPP (9): warning C4146: Univariate negative operators applied to unsigned types, the result is still unsigned
 Howwide. CPP (10): warning C4146: Univariate negative operators applied to unsigned types, and the result is still unsigned
 Howwide. CPP (11): warning C4146: Univariate negative operators applied to unsigned types, the result is still unsigned
 The link is in progress.
Clean up the intermediate garbage ___________.
Delete file - H: helloworld New folder vc80.idb
 Ready for operation

- New folder. exe
 Test results:
Floating point accuracy test 100 divided by 3
float            e=33.33333206176757800000000000000
double           f=33.33333333333333600000000000000
long double      g=33.33333333333333600000000000000
 The minimum values of signed numbers are
char a1   ->-128
short b1 =-32768
int c1   =-2147483648
long d1   =-2147483648
long long x1 =-9223372036854775808
 The lowest value is reduced by one, and the spillover is maximized.
char a1   ->127
short b1 =32767
int c1   =2147483647
long d1   =2147483647
long long x1 =9223372036854775807
 The maximum unsigned number (-1) is
unsigned char a2 ->255
unsigned short b2 =65535
unsigned int c2   =4294967295
unsigned long d2 =4294967295
unsigned long long x2 =18446744073709551615
 ... Maximum, add one, spill over, return to zero (minimum)
unsigned char a2 ->0
unsigned short b2 =0
unsigned int c2   =0
unsigned long d2 =0
unsigned long long x2 =0
 After zero minus one, it spills over again to get the maximum, so don't take countless numbers and less than 0.
unsigned char a2 ->255
unsigned short b2 =65535
unsigned int c2   =4294967295
unsigned long d2 =4294967295
unsigned long long x2 =18446744073709551615
 Storage size
 Sizeof (char) = 1 byte
 Sizeof (short) = 2 bytes
 Sizeof (int) = 4 bytes
 Sizeof (long) = 4 bytes
 Sizeof (long) = 8 bytes

Sizeof (unsigned char) = 1 byte
 Sizeof (unsigned short) = 2 bytes
 Sizeof (unsigned int) = 4 bytes
 Sizeof (unsigned long) = 4 bytes
 Sizeof (unsigned long) = 8 bytes

Sizeof (float) = 4 bytes
 Sizeof (double) = 8 bytes
 sizeof(long double) =8 bytes

-


Reproduced in: https://my.oschina.net/u/160145/blog/264399

Keywords: Windows less SDK Linker

Added by tibberous on Wed, 18 Sep 2019 08:42:45 +0300