VS Code配置用批处理编译C++
在VS Code中使用批处理编译的配置
用批处理单独处理编译程序,可以很方便的在VSCode,Editplus,Sublime Text等软件里配置编译功能。
使用VS 2022编译
从VS2015开始使用这个批处理,版本升级后只需要把VS的本机工具命令路径修改一下。
把下面内容保存为vc2022.bat
@echo off rem ============================= REM Visual C++ 2022 编译批处理 rem ============================= rem rem 调用参数 : "$(FilePath)" rem rem "$(FilePath)" 编译文件路径 rem rem SETLOCAL 开始批处理文件中环境改动的本地化操作 SETLOCAL cd /d "%~dp1" set OUTDIR=%~dp1exeOut set FILE=%~n1 set EXT=%~x1 if /I "%EXT%" NEQ ".cpp" ( if /I "%EXT%" NEQ ".c" ( echo 文件类型错误! goto END ) ) rem 切换到 VS2022 本机工具命令提示 再进行编译 call "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat" > NUL echo Visual C++ 2022 编译 %1 ... echo -------------------------------------------------------------------------------- @if NOT EXIST "%OUTDIR%" ( echo. echo 输出目录不存在,创建输出目录 "%OUTDIR%" ... md "%OUTDIR%" ) rem 编译选项 set CL=/MP /GS /analyze- /W3 /Gy /Zc:wchar_t /Gm- /Od /fp:precise /D "WIN32" /D "_DEBUG" /D "DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oy- /Oi /MD /Fe"%OUTDIR%/" /EHsc /nologo /Fo"%OUTDIR%/" /Fp"%OUTDIR%/" /Fd"%OUTDIR%/" /ZI echo. echo cl.exe %* %CL% ... cl.exe %* %CL% user32.lib gdi32.lib kernel32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib SET RETURN=%ErrorLevel% echo. if %RETURN% == 0 ( echo 编译成功! ) else ( echo 编译失败,请检查错误! ) echo. echo 清理垃圾文件 ... @if EXIST "%OUTDIR%\%FILE%.obj" (del "%OUTDIR%\%FILE%.obj") @if EXIST "%OUTDIR%\%FILE%.?db" (del "%OUTDIR%\%FILE%.?db") rem 编译成功后直接运行程序,vscode可以不用,像editplus、Sublime Text可以取消注释 rem if %RETURN% == 0 ( rem CD /d "%OUTDIR%" rem echo. rem echo 正在启动程序 %OUTDIR%\%FILE%.exe... rem START %OUTDIR%\%FILE%.exe rem ) :END rem ENDLOCAL 结束批处理文件中环境改动的本地化操作 ENDLOCAL
使用VC6编译
使用这个批处理不需要把vc添加到path中。
把下面内容保存为vc6.bat
@echo off rem ============================= REM VC++ 6 编译批处理 rem ============================= rem rem 调用参数 : "$(FilePath)" -S rem rem "$(FilePath)" 编译文件路径 rem -S 可选,不链接,只编译输出汇编文件.cod rem rem SETLOCAL 开始批处理文件中环境改动的本地化操作 SETLOCAL cd /d "%~dp1" set OUTDIR=%~dp1exeOut set FILE=%~n1 set EXT=%~x1 if /I "%EXT%" NEQ ".cpp" ( if /I "%EXT%" NEQ ".c" ( echo 文件类型错误! goto END ) ) rem 修改为你机子上VC的路径 CALL E:\Apull\Program\VC6\VC98\Bin\VCVARS32.BAT > nul echo VC++ 6.0 编译 %1 %2... echo -------------------------------------------------------------------------------- @if NOT EXIST "%OUTDIR%" ( echo. echo 输出目录不存在,创建输出目录 "%OUTDIR%" ... md "%OUTDIR%" ) set CL=/nologo /MD /W3 /Ox /EHsc /GX /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Fo"%OUTDIR%/" /Fe"%OUTDIR%/" /Fd"%OUTDIR%/" IF "%2" == "-S" ( SET CL=%CL% /FAc /Fa"%OUTDIR%/" /c ) echo. echo cl.exe %1 %CL% ... cl %* %CL% rem user32.lib gdi32.lib kernel32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib easyx*.lib SET RETURN=%ErrorLevel% echo. if %RETURN% == 0 ( echo 编译成功! ) else ( echo 编译失败,请检查错误! ) echo. echo 清理垃圾文件 ... @if EXIST "%OUTDIR%\%FILE%.obj" (del "%OUTDIR%\%FILE%.obj") @if EXIST "%OUTDIR%\%FILE%.pdb" (del "%OUTDIR%\%FILE%.pdb") rem 编译成功后直接运行程序 rem IF NOT "%2" == "-S" ( rem if %RETURN% == 0 ( rem CD /d "%OUTDIR%" rem echo. rem echo 正在启动程序 %OUTDIR%\%FILE%.exe... rem START %OUTDIR%\%FILE%.exe rem ) rem ) :END rem ENDLOCAL 结束批处理文件中环境改动的本地化操作 ENDLOCAL
使用GCC编译
把下面内容保存为gcc.bat
@echo off rem ============================= REM GCC 编译批处理 rem ============================= rem rem 调用参数 : "$(FilePath)" rem rem "$(FilePath)" 编译文件路径 rem rem Apull apull.net rem SETLOCAL 开始批处理文件中环境改动的本地化操作 SETLOCAL cd /d "%~dp1" set OUTDIR=%~dp1exeOut set FILE=%~n1 set EXT=%~x1 if /I "%EXT%" NEQ ".cpp" ( if /I "%EXT%" NEQ ".c" ( echo 文件类型错误! goto END ) ) set GCC=G++ set STD=-std=c++11 if /I "%EXT%" == ".c" ( set GCC=gcc set STD=-std=c11 ) echo. echo %GCC% 8.1.0 x64 编译 %1 ... echo -------------------------------------------------------------------------------- @if NOT EXIST "%OUTDIR%" ( echo. echo 输出目录不存在,创建输出目录 "%OUTDIR%" ... md "%OUTDIR%" ) rem 修改为你的gcc路径 set P=E:\Apull\Program\MinGW path=%P%\bin; rem 编译参数 set out=-o"%OUTDIR%\%FILE%.exe" set var=-Wall -O2 %STD% -fdiagnostics-color=auto echo. echo %GCC% %var% %* %out% call %GCC% %var% %* %out% echo. if %ErrorLevel% == 0 ( echo 编译成功! ) else ( echo 编译失败,请检查错误! ) echo. echo 清理垃圾文件 ... @if EXIST "%OUTDIR%\*.obj" (del "%OUTDIR%\*.obj") @if EXIST "%OUTDIR%\*.tds" (del "%OUTDIR%\*.tds") :END rem ENDLOCAL 结束批处理文件中环境改动的本地化操作 ENDLOCAL
配置task.json文件
需要修改文件路径为你电脑上的路径
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "g++", "type": "shell", "command": "E:/Apull/Program/gcc.bat", "args": [ "${file}", //"${fileDirname}/*.cpp", "-Ofast" ] }, { "label": "g++debug", "type": "shell", "command": "E:/Apull/Program/gcc.bat", "args": [ "${file}", "-g", "-Og" ] }, { "label": "VC2022", "type": "shell", "command": "E:/Apull/Program/vc2022.bat", "args": [ "${file}" ] }, { "label": "VC6", "type": "shell", "command": "E:/Apull/Program/vc6.bat", "args": [ "${file}" ] } ] }
运行效果