[原创]在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

 

posted @ 2019-11-28 20:15  sky0014  阅读(3656)  评论(0编辑  收藏  举报