[Xiaobai] Vscode configures Python and C++ environment

First, configure the python environment

The python written in pycharm before, vscode is slightly lightweight, the operation:

  • Ctrl+Shift+P or View > Command Palette, enter Python: Select Interpreter, select the python interpreter (you can use the anaconda environment)
  • Install the plugin:

insert image description here

  • You can interact/run code in the terminal: terminal->new terminal

Two, configure the C++ environment

Before learning C++, I ran the code on VS, and I can also use the lighter vscode. vscode is just a text editor. You only need to install the extension of the corresponding programming language, and you also need to install the corresponding compiler or interpreter.

First create a folder to store the code. It is recommended to use different folders for different programming languages, because after VSCode opens the folder (called the working directory), if a certain configuration is performed, a folder called ".vscode" will be generated under this folder. Some .json configuration files are stored in this folder, and these configuration files have an effect on the code files in the working directory. So when you need the same development environment in the future, you don’t need to create configuration files and perform related configurations every time, just copy the .vscode folder directly, but you still need to manually configure the environment you need for the first time.

2.1 Install MinGW compiler

There are many C/C++ compilers, you can choose the MinGW compiler.
Download address: MinGW compiler .

Note: It is required to modify the path name here,Make sure the path does not contain spaces and Chinese characters, especially spaces, because there are spaces in the default location, you must modify the corresponding installation path.

2.2 Install C/C++ extension

Search plug-ins, "C/C++". Remember to restart vscode.

2.3 Configure C/C++ environment

(1) Configure the compiler

Next, configure the compiler path, press the shortcut key Ctrl+Shift+P to bring up the command panel, enter C/C++, and select "Edit Configurations (UI)" to enter the configuration. Configure two options here: - Compiler path: D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe
insert image description here

(2) Configure build tasks

(1) Create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable from the source code. Press the shortcut key Ctrl+Shift+P to call up the command panel, enter tasks, and select "Tasks:Configure Default Build Task"; (
2) Then select "C/C++: g++.exe build active file", and a name will appear It is the configuration file of tasks.json;

{
    
    
    "tasks": [
        {
    
    
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\VSCODE\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
    
    
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

(3) Configure debug settings

(1) The main purpose here is to generate a launch.json file in the .vscode folder to configure debugging related information. Click Debug–>Start Debugging in the menu bar;
(2) Select C++ (GDB/LLDB), and then a launch.json file will be generated;
(3) Click the Add Configuration button to add the configuration yourself, or you can directly configure the following Copy the content of the json file in the past:

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\VSCODE\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

2.4 Test example

insert image description here

  • summary:
    • In fact, just pay attention to the generation of the next few json files, launch.jsonand tasks.jsonthe files can be automatically generated as long as [start debugging], and then press the bottom right corner of vscode Win32to generate c_cpp_properties.jsonfiles (note that you need to download the plug-in C/C++ before this Extension Pack).
    • The other is the storage of files, the header files Includeare stored in the folder .h, and then libvarious files are stored in the folder .cpp, and the final main.cppfile can be placed directly outside the two folders.
    • If there is another layer of files outside the opened file, the generated .vscodefolder may appear in the outer folder.

2.5 Precautions

  • According to the reference, the first configuration environment will be very troublesome, because the json configuration file created after ctrl+shift+p requires a lot of parameters to be filled in by yourself. The simplest generation and generation will be automatically generated through [Run] and [Start Debugging launch.json] tasks.json. , and then only add the header file and the corresponding source program with the tasks.jsonmodification ; if there is another file, press the bottom right corner of vscode and then press [Edit Configuration (JSON)].argscppc_cpp_properties.jsonwin32
  • Common reasons why vscode reports an error task c/c++: g++.exe generates an active file:
    • tasks.json defines the tasks to be executed, such as compiling tasks
    • launch.json defines the debugging tasks to be performed, such as debugging c/c++ programs
    • ctrl+shift+P to open Command Palette, run C/Cpp: Edit configurations... to generate c_cpp_properties.json
    • The default setting of includePath in c_cpp_properties.json is only the current directory, and system dependencies need to be added.
      Enter gcc -v -E -x c++ in the command line - find the header file directory in the result, and add it to the includepath.
    • The meaning of gcc with different parameters: "-g" generates debugging information, "-c" compiles the intermediate object file, "-I" specifies the link library, and "-o" generates the named executable file.

3. Plug-ins

  • codegeex plug-in: can "translate" code into code in different programming languages, AI automation to write comments, explain code and other functions
  • Vscode also has many interesting plug-ins that can be used, which will be briefly introduced next time.
  • Note: After downloading the plug-in, some need to restart vscode to use it.

Reference

[1] Official document: https://code.visualstudio.com/docs/languages/cpp
[2] https://zhuanlan.zhihu.com/p/87864677
[3] https://code.visualstudio.com/ docs/cpp/config-msvc
[4] Variable settings of vscode: https://code.visualstudio.com/docs/editor/variables-reference
[5] How to add and apply custom header files when writing C++ programs with VScode under Windows
[6] https://code.visualstudio.com/docs/cpp/config-clang-mac
[7] Vscode configuration c/c++ development environment under Mac

Guess you like

Origin blog.csdn.net/qq_35812205/article/details/123164286