Home >Development Tools >VSCode >One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

青灯夜游
青灯夜游forward
2023-02-27 19:33:445874browse

How to develop C/C in VScode? How to configure the C/C environment? The following article will share with you the VScode configuration C/C operating environment tutorial (nanny-level teaching). I hope it will be helpful to everyone!

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Preparation work

Configure the C/C running environment in Vscode. First, you need to download C /C development environment, and then add the C/C development environment to the system variables.

The first step is to download vscode

Everyone should be able to download VsCode, but VsCode is not provided here. Download and installation tutorial

The second step is to download mingw

Here mingw is used as C/C Development environment, the official website link is as follows

Official website link:MinGW official website

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

After the download is completed, we will get such an installation Program [Recommended learning: vscode tutorial, Programming teaching, vuejs video tutorial]

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Double-click to open

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

##Then we Find the installation folder of mingw

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

##Enter
in sequence

mingw-get install gcc
  • mingw-get install g
  • mingw-get install gdb
Note

: Because the server that provides the download service is abroad, the download will be slow, and some content may not even be downloaded (such as gdb.exe). If you do not want to use gdb for debugging, gdb.exe does not have to be downloaded. Yes, (the run code is compiled and run directly, no gdb debugger is required)

If you want to use the gdb debugger, please refer to the msys2 installation method provided by the boss in the comment area Install the latest version
or download it from Baidu Netdisk (don’t ask why you use Baidu Netdisk, because other netdisks are full of T_T)

The version provided here is version 8.1.0 (the latest version 11.2, as of 2022 /11/14)
Link:
MinGW 8.1.0 version
Extraction code: Suif Compressed package size 129.41MB, use Baidu Netdisk for a limited time experience of 30s (if you still have If yes), it will be downloaded soon
Personal test, version 8.1.0 fully supports C 17 and below standards.

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]##Then mingw download is completed

The third step is to add mingw to the system variables#This step is to tell the computer in which folder the C/C running environment can be found

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

#In this way, our system environment is configured

We can check whether the configuration is correct
Press and hold win r to open In the following interface

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

enter cmd, then click OK


One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching] and enter
gcc -v
g -v
gdb -v
See if normal results are displayed

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching] If the results are displayed normally, it means that the system environment is configured correctly. At this time, it is recommended to restart the computer (of course, it is not necessary Restart)

The fourth step is to open VsCode and install the necessary plug-ins

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Our vscode configuration C/C preparation work is completed

Officially start the configuration

The first step is to create a new folder to store C/C files and create a new cpp file

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

We open that folder in vscode

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

##Then we can start configuring the environment

Note

: The configured environment is only applicable In this large folder

Configuration method 1 Configuration method with the help of run code plug-in-simple

This is the simplest configuration method, and it is also the most friendly method for beginners or people with little contact with computers.
This method requires the use of a plug-in, run code. Let’s first Open the extension store and install the run code plug-in



. After the installation is completed, we will find that there is an additional run codeOne article explains in detail vscode configuration C/C++ running environment [nanny-level teaching] on the right click.

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

# Just click run code to compile and run the program.

Problem 1: When run code runs the program, data cannot be entered into the terminal

Due to the settings of run code Problem, some students may get output results when using run code to run the program, but cannot directly input content into the program in vscode, so we need some settings to adjust the run code.

We press ctrl to enter settings (ctrl comma) or click File-> Preferences-> Settings

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

##Restart vscode so that we can enter content into the program in vscode

Question 2: run code An error of gcc (or g) :error; no such file or directory appears when executing the code

Cause analysis

This error may be caused by the compile command not being executed before Caused before entering the specified folder, so we can add a cd target folder path

Solution

One step to open the code runner extension settings

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

The second step is to enter the command setting interface

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

The third step is to modify the compilation instructions

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

Configuration method 2 Configuration method using gdb debugging - complex

If you want to use the gdb debugger to debug the program, you need to use this method

The first step is to configure the editor environment

We hold down ctrl shift p to open the command panel or click View-> command Panel

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

We configure the compiler path and c/c standard

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Then close this interface, we will find that there is an additional .vscode folder in the CppProject folder

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

The second step is to configure task.json compilation task

Similarly, we hold down ctrl shift p to enter the command Panel or click View-> Command Panel

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

At this time we will find that there are more tasks.json## in the .vscode folder

# Briefly introduce what you need to pay attention to in tasks.json:

    label
  • : Compile the task name and configure launch.json afterwards We can call the compilation task by the compilation task name.
  • command
  • : Compiler path, the compilation task will call the compiler, please make sure the path is correct. args
    : Command transmitted to the compiler. By setting this content, you can set the path to generate the exe program

The third step is to configure the launch.json debugging task

Because the current C/C plug-in will not automatically generate launch.json, so launch .json needs to be written by ourselves. Create a new launch.json file in the .vscode folder

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

We will find that there is an added configuration

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

We click Add Configuration and select gdb to start

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

We Three places need to be modified

The first place: program - executable program path

  • This content indicates the called program path , its value should be the same as the path of the exe program generated in task.json, otherwise an error will be reported because the executable program cannot be found

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Second place: miDebuggerPath - Custom debugger path

  • This value represents the path of the gdb debugger. The value should be consistent with the path of the gdb debugger you want to use, otherwise it will prompt that the gdb debugger cannot be found

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

##The third place:

preLaunchTask - Compilation task executed before debugging

    This value indicates the compilation task used before starting the gdb debugger, and its value Equal to label in task.json.
  • This content is not automatically generated, so we need to write it manually.

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Save and restart the editor, we can press F5 to debug the program

Note: Any changes made to the file need to be saved before the new content will take effect. We can press ctrl s to quickly save the file

Question 1 Why is there no output result after running the program?

Q: Why does this error message appear after pressing F5 and there is no output result

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Answer: This is not an error message, but a compilation and debugging command, and the output results are displayed in the debugging console.

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

Advanced setting is the classic pop-up black window running program (in the system terminal Run the program in)

Some friends may find that the debugging console is not easy to use. If you want vscode to compile and run a c/c program, a terminal will pop up and run the program in the terminal. This Clicking is actually very simple. We only need to change the contents of program and args in launch.json, and then set the black window to pop up.

Q: Why not set it to run the program in the vscode built-in terminal?

Answer: Using gdb debugging with vscode does not provide configuration items for running programs in the built-in terminal of vscode.

    If you want to set up the program to run in the vscode built-in terminal, please refer to configuration method one or configuration method three

Note : Using the system terminal (cmd) to run the program cannot use breakpoint debugging, so please configure it as appropriate

It is not necessary for people like me who use vscode to write algorithm questions. breakpoint, so it is better to configure it to pop up a black window.

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

我的program 和 args 中的内容:

        "program": "C:\\Windows\\System32\\cmd.exe",
        "args": [
            "/c",
            "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", // 更改这项内容为task.json中生成的exe程序路径就好
            "&",
            "pause"
        ],

完成后的效果
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

配置方法三 使用windows调试 - 比较复杂

有的小伙伴不想用run code,但是又想达到run code那样能在vscode内置终端中执行程序的效果,可以尝试下使用windows调试。(实际上就是只编译运行程序,没有任何辅助调试程序的功能)

注意:该配置方法无法使用断点调试。

第一步 配置编辑器环境

同配置方法二中的第一步

第二步 配置task.json 编译任务

同配置方法二中的第二步

第三步 配置launch.json 调试任务

先自建一个lanuch.json文件(参考配置方法二中的第三步)

  • 如果lanuch.json中有内容的话,先把configurations中的内容注释掉或者删除掉
  • 全选configurations中的内容,按下快捷键ctrl + / 即可快速注释选中内容。

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

然后我们点击添加配置,选择windows启动

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

我们只需要修改两项内容即可

第一处:program - 可执行程序路径

  • 该项内容表示调用的程序路径,其值应和task.json中生成的exe程序路径相同,否则会由于无法找到可执行的程序而报错

第二处:preLaunchTask - 调试之前执行的编译任务

  • 该项值表示启动gdb调试器之前使用的编译任务,其值等于task.json中的label。
  • 该项内容并不会自动生成,因此需我们手动编写。

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

注意配置项: console - 启动调试目标的位置

  • 通过更改该项的值,我们可以自由地切换在系统终端中运行程序还是在vscode终端中运行程序
  • 该项值为 externalTerminal 则是在系统终端中运行程序
  • 该项值为 integratedTerminal 则是在vscode终端中运行程序

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

我们把该项值改为 integratedTerminal 即可在vscode终端中运行程序

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

最终效果:

One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]

全篇结束,感谢阅读!如果有任何疑问可以评论区留言(因为水平有限,有些问题不一定能解答哈)!

编辑器插件推荐:

  • GitHub Theme: Theme plug-in, the editor theme shown in this article, Github Light pure white looks very comfortable.
  • vscode-icons: Icon plug-in, the icons shown in this article, with the icons, the sidebar looks more vivid.
  • codeSnap: A tool plug-in that generates code screenshots and is used to analyze the code. It is very nice.

For more knowledge about VSCode, please visit: vscode Basic Tutorial!

The above is the detailed content of One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete