VSCode configures C/C++ development environment ( MSVC )

0. Green version

  • The green version uses VSCode User 1.7.4.2 X64 version.

  • The build tool used by the green portable version comes from the 32-bit tool chain provided by the Visual C++ 2010 version.

  • Win 7 Win10 Win 11 works fine.

  • How to use it: After downloading the file, unzip it, run the VCVars.bat file as an administrator, and then run ShortCut.bat so that a desktop shortcut will be generated.

  • Open the given Code folder with VSCode, you can quickly compile and run the example. For details, please refer to step 1.4 in manual configuration.

  • The file structure diagram of the green version compressed package is given below:

VSCode-win32-x64-1.74.2
├ Code <folder>
├ VCompiler <folder>
│ ├ include <folder>
│ │ ├ easyx.h
│ │ └ graphics.h
│ ├ bin <folder>
│ │
│ └ Lib <folder>
├ ShortCut.bat ├ EasyXa.lib
├ VCVars .bat └ EasyXw.lib
└ 其他文件

1. Manual configuration

1.1 Configuration of plug-ins and environment variables

  • After installation, click the fifth button on the left as shown in the figure below. Chinese is used here. If you like English, you can skip this step. Here we take Chinese as an example.

  • Download the VC generation tool, here we take the X86 bit generation toolchain extracted from VC 2010 as an example, with the built-in EasyX library, version 20220901

  • Download link: https://www.aliyundrive.com/s/S7J5ajSYB4j and decompress it by yourself. Here we take decompression to disk D as an example. (If you want to update VC2022, you can refer to the third section)

  • Download and install the C/C++ plugin in VSCode, then restart VSCode.

  • Press the Ctrl+Shift+P key combination and enter C/C++: Edit configuration (JSON) item, as shown in the following figure:

  • After left-clicking, it will enter the configuration file of c_cpp_properties.json. This file is to configure the intellisense of VSCode C++, providing code prompts, error prompts and other functions. Clear the code inside and enter the following code:

{
    "configurations":[
        {
            "name":"Win32",
            "includePath":
            [
                "${workspaceFolder}/**"
            ],
            "defines":
            [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "cl.exe",
            "cStandard": "c99",
            "cppStandard": "c++11",
            "intelliSenseMode":"windows-msvc-x86"                    
        }
    ],
    "version":4
}

1.2 Corresponding sample code

  • Create a folder Code under the D disk, open it with VSCode, then create a new file circle.cpp, and enter a simple circle drawing code in it, as follows:

#include<graphics.h>#include<conio.h>intmain(){
	initgraph(640,480);
	circle(320,240,50);
	_getch();
	return0;}

1.3 Tasks and run configurations

  • Go back to the circle.cpp editing page, click "Configuration Task" under the Terminal tab, there will be an option "C/C++:cl.exe" to generate the active file, as shown in the figure:

  • If the first option above is not available, select the second option "Create tasks.json using a template" and click "other". No matter which one you choose, you will enter the editing page of task.json, clear the code in this file, and add the following code:

{
	"version": "2.0.0",
	"tasks":
	[
		{
			"type":"cppbuild",
			"label":"C/C++:Debug",
			"command":"cl.exe",
			"args":
			[
				"/Zi",
				"/EHsc",
				"/nologo",
				"/MDd",
				"/Debug",
				"/Fe${fileDirname}\\Debug\\${fileBasenameNoExtension}.exe",
				"/Fo${fileDirname}\\Debug\\${fileBasenameNoExtension}.obj",
				"/Fd${fileDirname}\\Debug\\${fileBasenameNoExtension}.pdb",
				"/DUNICODE",
				"/D_UNICODE",
				"/D_DEBUG",
				"${file}",
				"user32.lib",
				"kernel32.lib",
				"shell32.lib"
			],
			"options":
			{
				"cwd":"${fileDirname}"
			},
			"problemMatcher":
			[
				"$msCompile"
			],
			"group":"build",
			"detail":"MSVC Compiler"
		},
		{
			"type":"cppbuild",
			"label":"C/C++:Release",
			"command":"cl.exe",
			"args":
			[
				"/Zi",
				"/EHsc",
				"/nologo",
				"/MT",
				"/Fe${fileDirname}\\Release\\${fileBasenameNoExtension}.exe",
				"/Fo${fileDirname}\\Release\\${fileBasenameNoExtension}.obj",
				"/Fd${fileDirname}\\Release\\${fileBasenameNoExtension}.pdb",
				"/DUNICODE",
				"/D_UNICODE",
				"/DNDEBUG",
				"${file}",
				"user32.lib",
				"kernel32.lib",
				"shell32.lib"
			],
			"options":
			{
				"cwd":"${fileDirname}"
			},
			"problemMatcher":
			[
				"$msCompile"
			],
			"group":"build",
			"detail":"MSVC Compiler"
		}
	]
}
  • Go back to the editing page of circle.cpp, click the "Run" tab under the "File menu" to add configuration, there will be an option box, select the "C/C++ (Windows)" option, and then you will enter the launch.json Edit the interface, clear the code inside, and enter the following code:

{
	"version":"0.2.0",
	"configurations":
	[
		{
			"name":"开始调试 (Debug) ",
			"type":"cppvsdbg",
			"request":"launch",
			"program": "${fileDirname}\\Debug\\${fileBasenameNoExtension}.exe",
			"args":[],
			"stopAtEntry": false,
			"cwd": "${fileDirname}",
			"environment": [],
			"console": "externalTerminal",
			"preLaunchTask":"C/C++:Debug"
		}
	]

}

1.4 Compile and debug run sample code

  • After completing the configuration of the above files, we can compile the files. Go back to the circle.cpp editing page, click Run Build Task under the "Terminal" tab or press the Ctrl+Shift+B key combination, as shown in the figure:

  • Select the first "C/C++:Debug" to compile in Debug mode, and select the second one to compile in Release mode. Here select the first option in the illustration, after clicking, the output box of "Terminal" below will have the following words:

* 正在执行任务: C/C++:Debug
正在启动生成...
cl.exe /Zi /EHsc /nologo /MDd /Debug /FeD:\Code\Debug\circle.exe /FoD:\Code\Debug\circle.obj /FdD:\Code\Debug\circle.pdb /DUNICODE /D_UNICODE /D_DEBUG D:\Code\circle.cpp user32.lib kernel32.lib shell32.lib
circle.cpp
生成已成功完成。
* 终端将被任务重用,按任意键关闭。
  • 运行完毕后,左侧文件目录框,就会生成一个 Debug 文件夹,里面的 circle.exe 就是我们刚刚编译的文件啦。

  • 然后点击“运行”选项卡的“非调试模式运行”或者按下 Ctrl+F5 组合键就可以运行程序。

  • 点击“运行”选项卡的“启动调试“或者按下 F5 则就是调试程序,在 .cpp 文件行号左侧点击就是下断点,断点处有红点标记。此时 VSCode 左侧会变成变量监视框和堆栈浏览框。

  • 手动配置教程结束,感谢您的观看!

2.相应官方文档:

3.更新的 VC 生成工具-VC2022

3.1 版本和下载地址

  • VC 生成器版本号:14.34.31933

  • 搭配的 WindowsSDK 版本号:10.0.20348.0

3.2 配置方法

  • 以 64 位计算机,编译 64 位程序的需求为例

3.2.1 修改环境变量

Path:

  • D:\VC2022\MSVC\bin\Hostx64\x64

Include:

  • D:\VC2022\MSVC\Include

  • D:\VC2022\MSVC\atlmfc\include

  • D:\VC2022\MSVC\WindowsKits\Include\um

  • D:\VC2022\MSVC\WindowsKits\Include\ucrt

  • D:\VC2022\MSVC\WindowsKits\Include\shared

  • D:\VC2022\MSVC\WindowsKits\Include\winrt

  • D:\VC2022\MSVC\WindowsKits\Include\cppwinrt

Lib:

  • D:\VC2022\MSVC\lib\x64

  • D:\VC2022\MSVC\WindowsKits\Lib\um\x64

  • D:\VC2022\MSVC\WindowsKits\Lib\ucrt\x64

3.2.2 修改 c_cpp_properties.json,修改后可以获得更好的代码提示,错误提示等功能

{
	"configurations":[
		{
			"name":"Win32",
			"includePath":
			[
				"${workspaceFolder}/**"
			],
			"defines":
			[
				"_DEBUG",
				"UNICODE",
				"_UNICODE"
			],
			"compilerPath":"cl.exe",
			"cStandard":"c17",
			"cppStandard":"c++17",
			"intelliSenseMode":"windows-msvc-x64"					
		}
	],
	"version":4}

3.2.3 注意事项

  • 其他操作同第一节的配置

  • 已经内置 EasyX 20220901

  • 支持 ATL,不支持 MFC

  • 32 位计算机请使用 D:\VC2022\MSVC\bin\Hostx86\X86,Include 和 Lib 中的 X64 请改为 X86

4.已有 Visual Studio 的简便方法

4.1 使用 VSCode 插件 CMake

  • 安装 下图所示插件:

4.1.2 使用方法

  • 以在 D 盘下的 example 文件为例

  • 用 VSCode 打开该文件后,按下 Ctrl+Shift+P 组合键,然后如下图所示

  • 选择第一个,然后 CMake 就会自动搜寻电脑上安装的 VC 系列或者 GCC 系列工具链。如下图所示(笔者电脑上是安装了 VS2022 生成工具):

  • 此处选择 Visual Studio 生成工具 2022 Release -X86,选择后,会有如图所示图样:

  • Library 是选择生成库文件,Executable 是选择生成可执行文件 (.exe),选择第二个选项

  • 选择后会进入到 CMakeList.txt 文件的编辑页面,熟悉 CMake 的可以直接写 CMake 相关命令

  • 在生成 CMakeLists.txt 的同时还会有 main.cpp 的生成,进入这个源文件的编辑界面,可以发现已经自动写好了一个 Hello World 程序

  • 将视线移到下面如图所示的栏目条

  • 左侧第的 “CMake:[Debug]:Ready" 按钮是表示 CMake Debug 模式就绪,点击它可以切换 release 等模式

  • 更左侧的一个按钮是切换工具链,具体可以亲自点击查看

  • Build 标签的按钮是编译生成的功能类似于 VS 的生成解决方案

  • [ALL_BUILD] 在熟练掌握 CMake 后,自然就会了解,这里不展开讲解

  • [ALL_BUILD] 按钮右侧旁边的两个按钮,都是运行程序的功能,一个是不在终端运行,一个是在 VSC 里的内置终端窗口运行,具体可以亲自尝试,感受不同

4.3 使用 EasyX 的注意事项

  • 如果 Visual Studio 中已经配置了 Easyx ,不用在 CMakeLists.txt 中写入相关链接库的操作


END


今天的分享到此结束了,如果在编程学习的路上遇到问题,不妨来我的编程学习交流基地一起来学习探讨~

Guess you like

Origin blog.csdn.net/weixin_58045538/article/details/128834014