有人学了三个月没写过helloworld;有人不知道编译器是什么;有人第一个helloworld编译一下,数百个ERROR,自信心失去一大半.
学习c++,想试验一下习题,不知道如何入手,你还在IDE里面重复新建工程,然后产生一大片垃圾文件吗.
其实还有另外一种编译方式:
那就是传说中的记事本加命令行编译.只要你有U盘,只有装windows的电脑,就可以写代码了.
一.首先你要有Platform SDK
WIN7 Windows SDKS微软官方下载页
1.GRMSDK_EN_DVD.iso X86(32bit)
2.GRMSDKIAI_EN_DVD.iso I64(Itanium)
3.GRMSDKX_EN_DVD.iso AMD64(64bit)
二.安装完,然后在安装的根目录(含有PlatformSDK,lib,include目录)下新建一个cmd文件内容如下 (以VC8.0为例)
@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 ******** 欢迎进入C/C++编译模式**********
@echo ┏┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┓
@echo ┋ VC 2005 编译器 ┋
@echo ┋ -------------------------------------- ┋
@echo ┋ rc.exe:资源编译器 ┋
@echo ┋ ml.exe:宏汇编编译器 ┋
@echo ┋ cl.exe:C/C++编译器 ┋
@echo ┋ link.exe:链接器 ┋
@echo ┋ lib.exe:库生成器 ┋
@echo ┋ nmake.exe:(makefile)程序维护实用工具 ┋
@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 设置调试符号路径
@set _NT_SYMBOL_PATH=%vc2005dir%\myTOOL\windbg\symbols
@echo VC2005所在根目录是%vc2005dir%
@echo (若上面目录不正确将不能正确配置环境变量path,include,lib)
@echo 请以 cd /d 命令转至源程序目录,然后开始编译等工作
echo.
PROMPT -
if "#%once_defined%"=="#" ( set once_defined=true&&%comspec% /k)
敲入CL /? LINK /?确定是否正确
3.每次敲命令是不是很烦,应该还有另一种方式生成目标,新建一文档Create_VC_UNICODE_v2.0.CMD,然后添加VC路径
if exist D:\software\DevKit\VC set vc2005dir=D:\software\DevKit\VC
@title VC8.0命令行生成WIN32,UNICODE,RELEASE版
@rem by wisepragma
@PROMPT -
@ECHO off
@echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rem color 0a
color f6
rem 在这里配置vc的根目录
if exist D:\软件\VC8.0NEW set vc2005dir=D:\软件\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 不要有空格或中文,会出错,有乱码
rem 取当前目录名作为程序名
for %%i in (.) do set outfile=%%~ni
rem set outfile=color
set outdir=.\
set cedir=temp
set cd0=%cd%
@echo 正在清除垃圾...
>>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 正在编译资源...
for %%x in (*.rc) DO rc /v %%x
@echo 正在编译源文件...
rem /D "_ATL_MIN_CRT" 会产生ERROR:LNK2005 XXX已经在 atlmincrt.lib(atlinit.obj) 中定义
>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 链接正进中...
>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 判断
if exist %outfile%.exe goto goahead
:ABORT
rem COLOR 0c
rem color f0
color f3
cd %cd0%
%comspec% /k
:goahead
echo 清除中间垃圾...
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 ------准备运行--------
@echo on
%outfile%.exe
@rem start %outfile%.exe
@ECHO.
@%comspec% /k
到此我们就可以编译helloworld.cpp了
#include <iostream>
using namespace std;
int main()
{
cout<<"hellowrold"<<endl;
}
将源文件单独放在新建的目录下,把Create_VC_UNICODE_v2.0.CMD也拷进入,双击CMD自动生成
再来个示例代码
//C++数值类型的取值范围
#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
//以下取-1转化后为最大值
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位有效数字-7
double f=100.0/3;//10e±308,12位有效数字-16
long double g=100.0/3;//10±4932,15位有效数字-16
cout.precision(50);
cout<<"测试平台:Intel P4,XPsp3-32bit,VC8.0"<<endl;
cout<<"浮点数精度测试100除以3"<<endl;
cout<<"float \t\t e="<<e<<endl;
cout<<"double \t\t f="<<f<<endl;
cout<<"long double \t g="<<g<<endl;
cout<<"有符号数取最小值分别是"<<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<<"....最低值再减一,溢出得最大值"<<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<<"无符号数取最大值(-1)分别是"<<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<<"...最大值,加一后,溢出,归零(最小值)"<<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<<"....零减一后,又溢出,得最大值,所以别拿无数号数和小于0比"<<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<<"存储大小"<<endl;
cout<<"sizeof(char) ="<<sizeof(char)<<"字节" <<endl;
cout<<"sizeof(short) ="<<sizeof(short) <<"字节" <<endl;
cout<<"sizeof(int) ="<<sizeof(int)<<"字节" <<endl;
cout<<"sizeof(long) ="<<sizeof(long )<<"字节" <<endl;
cout<<"sizeof(long long) ="<<sizeof(long long)<<"字节" <<endl<<endl;
cout<<"sizeof(unsigned char) ="<<sizeof(unsigned char)<<"字节" <<endl;
cout<<"sizeof(unsigned short) ="<<sizeof(unsigned short )<<"字节" <<endl;
cout<<"sizeof(unsigned int) ="<<sizeof(unsigned int )<<"字节" <<endl;
cout<<"sizeof(unsigned long) ="<<sizeof(unsigned long )<<"字节" <<endl;
cout<<"sizeof(unsigned long long) ="<<sizeof(unsigned long long)<<"字节" <<endl<<endl;
cout<<"sizeof(float) ="<<sizeof(float )<<"字节" <<endl;
cout<<"sizeof(double) ="<<sizeof(double)<<"字节" <<endl;
cout<<"sizeof(long double) ="<<sizeof(long double )<<"字节" <<endl;
//其他方法#include "limits"//其他方法
/*
cout<<"---------以下最大值--------------------"<<endl;
cout<< numeric_limits<unsigned long long >::max() <<endl;
cout<< numeric_limits< long long >::max() <<endl;
cout<<"---------以下最小值--------------------"<<endl;
cout<< numeric_limits<unsigned long long >::min() <<endl;
cout<< numeric_limits<long long>::min() <<endl;
*/
//long long 取值-2E(sizeof(long long)*8)/2 ~ 2E(sizeof(long long)*8)-1
//unsigned long long 取值0~2E(sizeof(unsigned long long)*8)-1
return 0;
}
运行结果
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
正在清除垃圾...
找不到 H:\helloworld\新建文件夹\cl_set.rsp
错误: 没有找到进程 "新建文件夹.exe"。
删除文件 - H:\helloworld\新建文件夹\新建文件夹.exe
子目录或文件 .\ 已经存在。
++++++++++Compiling and linking+++++++++++++++++++++++++++++++++++++
正在编译资源...
正在编译源文件...
howwide.cpp
howwide.cpp(9) : warning C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型
howwide.cpp(10) : warning C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型
howwide.cpp(11) : warning C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型
链接正进中...
清除中间垃圾...
删除文件 - H:\helloworld\新建文件夹\vc80.idb
------准备运行--------
-新建文件夹.exe
测试结果:
浮点数精度测试100除以3
float e=33.33333206176757800000000000000
double f=33.33333333333333600000000000000
long double g=33.33333333333333600000000000000
有符号数取最小值分别是
char a1 ->-128
short b1 =-32768
int c1 =-2147483648
long d1 =-2147483648
long long x1 =-9223372036854775808
....最低值再减一,溢出得最大值
char a1 ->127
short b1 =32767
int c1 =2147483647
long d1 =2147483647
long long x1 =9223372036854775807
无符号数取最大值(-1)分别是
unsigned char a2 ->255
unsigned short b2 =65535
unsigned int c2 =4294967295
unsigned long d2 =4294967295
unsigned long long x2 =18446744073709551615
...最大值,加一后,溢出,归零(最小值)
unsigned char a2 ->0
unsigned short b2 =0
unsigned int c2 =0
unsigned long d2 =0
unsigned long long x2 =0
....零减一后,又溢出,得最大值,所以别拿无数号数和小于0比
unsigned char a2 ->255
unsigned short b2 =65535
unsigned int c2 =4294967295
unsigned long d2 =4294967295
unsigned long long x2 =18446744073709551615
存储大小
sizeof(char) =1字节
sizeof(short) =2字节
sizeof(int) =4字节
sizeof(long) =4字节
sizeof(long long) =8字节
sizeof(unsigned char) =1字节
sizeof(unsigned short) =2字节
sizeof(unsigned int) =4字节
sizeof(unsigned long) =4字节
sizeof(unsigned long long) =8字节
sizeof(float) =4字节
sizeof(double) =8字节
sizeof(long double) =8字节
-