[原創]在Windows平台使用msvc(cl.exe) + vscode編寫和調試C/C++代碼


1、在.vscode目錄下,新建以下幾個配置文件,當然也可以通過vscode命令自動生成,如果你已有這些文件直接修改即可。

 

c_cpp_properties.json(代碼提示):

 1 {
 2   "configurations": [
 3     {
 4       "name": "Win32",
 5       // 設置windows sdk版本
 6       "windowsSdkVersion": "10.0.18362.0",
 7       // 設置msvc編譯器路徑
 8       "compilerPath": "D:/ProgramData/Microsoft/VisualStudio/IDE/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe",
 9       // 智能提示會從這里搜索頭文件
10       "includePath": [
11         "${workspaceFolder}/third_party/SDL2-2.0.10/include",
12         "${workspaceFolder}/third_party/ffmpeg-4.2-win64/include"
13       ],
14       "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
15       // 使用msvc類型的智能提示
16       "intelliSenseMode": "msvc-x64"
17     }
18   ],
19   "version": 4
20 }

 

tasks.json(編譯):

 1 {
 2   "tasks": [
 3     {
 4       "type": "shell",
 5       // 標示名
 6       "label": "msvc build",
 7       // 要執行的命令,這里用了一個bat腳本來構建
 8       "command": "build.bat",
 9       // 命令參數
10       "args": ["${fileBasename}", "build", "${fileBasenameNoExtension}"],
11       "group": {
12         "kind": "build",
13         "isDefault": true
14       },
15       "presentation": {
16         "echo": true,
17         "reveal": "always",
18         "focus": false,
19         "panel": "shared",
20         "showReuseMessage": true,
21         "clear": false
22       },
23       "problemMatcher": "$msCompile"
24     }
25   ],
26   "version": "2.0.0"
27 }

 

launch.json(調試):

 1 {
 2   // 使用 IntelliSense 了解相關屬性。
 3   // 懸停以查看現有屬性的描述。
 4   // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
 5   "version": "0.2.0",
 6   "configurations": [
 7     {
 8       "name": "(msvc) Launch",
 9       "type": "cppvsdbg",
10       "request": "launch",
11       // 要調試的程序
12       "program": "${fileBasenameNoExtension}.exe",
13       // 程序運行參數
14       "args": ["../sample.mp4"],
15       // 是否在入口自動斷點
16       "stopAtEntry": false,
17       // 工作目錄
18       "cwd": "${workspaceFolder}\\build",
19       "environment": [],
20       "externalConsole": false,
21       // 先編譯,再調試,對應tasks.json中的標示
22       "preLaunchTask": "msvc build"
23     }
24   ]
25 }

 

settings.json(vscode終端配置):

1 {
2   // 要使用cmd終端,其他終端會有兼容問題
3   "terminal.integrated.shell.windows": "C:/WINDOWS/System32/cmd.exe"
4 }

 

2、在根目錄下創建構建腳本,構建腳本主要是配置msvc環境,配置cl.exe的構建參數(頭文件、鏈接庫等)

 build.bat(構建腳本,可根據需要自行更改):

 1 @echo off
 2 
 3 REM %1: target file with ext
 4 REM %2: build path
 5 REM %3: output file without ext
 6 
 7 REM https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2019
 8 call "D:\ProgramData\Microsoft\VisualStudio\IDE\VC\Auxiliary\Build\vcvarsall.bat" x64 10.0.18362.0
 9 
10 REM create build directory
11 if not exist %2 md %2
12 
13 set compilerflags=/Od /Zi /EHsc /Fd%2\ /Fo%2\
14 
15 set linkerflags=/OUT:%2\%3.exe
16 
17 REM includes
18 set includes=
19 for %%i in (^
20     third_party/SDL2-2.0.10/include,^
21     third_party/ffmpeg-4.2-win64/include) do call :concat %%i
22 
23 REM libs
24 set libs=^
25     third_party/SDL2-2.0.10/lib/x64/SDL2main.lib^
26     third_party/SDL2-2.0.10/lib/x64/SDL2.lib^
27     third_party\ffmpeg-4.2-win64\lib\avformat.lib^
28     third_party\ffmpeg-4.2-win64\lib\avcodec.lib^
29     third_party\ffmpeg-4.2-win64\lib\avutil.lib
30 
31 REM compile with msvc
32 cl.exe %compilerflags% %libs% %1 %includes% /link %linkerflags%
33 
34 goto :eof
35 
36 REM concat includes path
37 :concat
38     set includes=%includes% /I %1
39 goto :eof

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM