VS code configuration C/C++ compilation environment basic tutorial and "the file has been downloaded incorrectly" error resolution

foreword

VSCode is an easy-to-use open source editor. One of its good reputation sources is that it has many plug-ins, which are lightweight and extremely scalable to achieve various functions. It can be easily and multi-functionally integrated. Edit documents in multiple formats, and C/C++ is of course no exception. The C/C++ extension tools provided by VS code do not include compilers or debuggers. So you need to install these tools yourself.

  • Using mingw-w64
    mingw-w64 1 is a complete and compact development environment that supports Windows operating systems. It actually ported the classic open source C language compiler GCC to the Windows platform. Some well-known open source IDEs actually just encapsulate MinGW-w64, so that it has a friendly graphical interface and simplifies operations, but the internal core is still MinGW-w64

First of all, you need to have a C/C++ operating environment, which can be achieved by installing plug-ins in VSCode. The plug-in interface can be found on the left side of the interface. From top to bottom are resource manager, search, source code management, debug and run, extension plug-ins. Test:
insert image description here
Enter the corresponding plug-in name in the search box to find the "C/C++" plug-in extension, and click to install it. In addition, if you need Sinicization, you can search for the "Chinese" Sinicization plug-in to install it.

Now you have an environment to run C/C++, but there is still a long way to go to run the code.

First, create a new folder in the resource manager, right-click it, click "open with Code" or "open with Code", we can see the "open editor" and the folder you opened in the upper right corner.
insert image description here
After that, just write a program such as helloworld, press F5 to run it, and when you encounter the default option of selecting the environment, you will see a launch.json automatically pop up, and a new .vscode folder and launch.json.

Then right-click the .vscode folder, create a new file, name it "tasks.json" and
insert image description here
paste the following code in launch.json (overwrite and change the original code, if you are worried, you can comment out the original one and add a new one):

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",	//要运行的文件
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MINGW\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "complie" // 调试会话开始前执行的任务,类似于Ant,这里为我们的编译任务
        }
    ]
}

It should be noted that the value after "miDebuggerPath" needs to be changed to the path where your gdb is located ( here the path should use "\" ).
Here is my path:
insert image description here

Mingw-w64 installation

Introduction to MinGW

The full name of MinGW is: Minimalist GNU on Windows. It ported the classic open source C language compiler GCC to the Windows platform and included Win32API, so the source code can be compiled into an executable program that can run on Windows.

And you can also use some development tools under the Linux platform that Windows does not have.

In one sentence: MinGW is the Windows version of GCC .

The difference between MinGW-w64 and MinGW

The difference between MinGW-w64 and MinGW is that MinGW can only compile and generate 32-bit executable programs, while MinGW-w64 can compile and generate 64-bit or 32-bit executable programs.

Because of this, MinGW has been replaced by MinGW-w64, and MinGW has already stopped updating. The built-in GCC is stagnant at version 4.8.1, while the built-in GCC of MinGW-w64 has been updated to version 8.1.0.

MinGW-64 download

official website

Drop down to select:
insert image description here

MinGW-w64 installation

Step 1: Double-click MinGW-W64-install.exe

Step 2: Select the installation information

Version: refers to the version of gcc. If there is no special requirement, generally choose the highest version number. The highest version is 8.1.0, just select it

choose default

**Architechture:**If the computer system is 64-bit, select x86_64; if it is a 32-bit system, select i686

Choose x86_64

**Threads:**If it is Windows, choose win32, if it is Linux, Unix, Mac OS and other operating systems, choose posix

choose win32

Exception : seh is new, but sjlj is ancient. seh has better performance, but does not support 32-bit. sjlj has good stability and supports 32 bits.

It is recommended to choose seh for 64-bit operating system

choose seh
insert image description here

The third step: the final choice is as follows:
insert image description here
the fourth step: just go to the next step.

"the file has been downloaded incorrectly!" error solution

Solution:
The offline installation solution is as follows:

  • Install MinWG-w64 offline
    Open the official website and select the version you need:
    insert image description here
    After downloading, extract it to any directory (the path should not contain Chinese):
    - Add /bin after the system environment variable

Open the Path of the system variable: insert image description here
insert image description here
Copy the address of the bin folder of MinGW-w64 to the newly added blank variable location to complete the configuration of MinGW-w64; 5 Verify that the
installation is successful
Open the cmd window, enter gcc -v and press Press the Enter key, as shown in the figure below, the installation is successful.
insert image description here
So far, all basic configurations have been completed.

Paste the following code in tasks.json (again, comment out the original if you are not at ease):

{
    
    
    "version": "2.0.0",
    "tasks": [
        {
    
    
            "label": "complie",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${fileBasename}",
                "-o",
                "${fileBasenameNoExtension}.exe"
            ],
            "presentation": {
    
    
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

Create a new c++ program with a cpp suffix:

#include <iostream>
using namespace std;
int main()
{
    
    
    cout << "Hello, world!" << endl;
    return 0;
}

After that, press F5 to run the program. For the first time, you may need to wait for a while or run it again.
insert image description here
Run successfully!

Guess you like

Origin blog.csdn.net/weixin_48936263/article/details/124799632