Run c++ code in vscode, windows 11 system

origin

I use a Mac computer at work and have vscode and c++ installed. I also want to run vscode on my windows computer at home, but I encounter a problem at this time. Now record the solution to the problem for reference.

solution

I was lazy at first, so I just searched Baidu and found a few articles, but nothing worked. Later I went to the official website. Because I remember that the tutorials on the official website are the most intuitive. If you are too lazy to listen to my ramblings, just go to the official website and read it.
Official website address
https://code.visualstudio.com/docs/languages/cpp

Prerequisites
To successfully complete this tutorial, you must perform the following steps:

Install Visual Studio Code.

Install the C/C++ extension for VS Code. You can install C/C++ extensions by searching for "C++" in the Extensions view (Ctrl+Shift+X).
Insert image description here

Install MinGW-w64 toolchain

Get the latest version of MinGW-w2 via MSYS64, which provides the latest native versions of GCC, MinGW-w64, and other useful C++ tools and libraries. This will provide you with the necessary tools to compile your code, debug it, and configure it to use IntelliSense.

  1. You can download the latest installer from the MSYS2 page or use this direct link tothe installer.

  2. Run the installer and follow the steps of the installation wizard. Please note that MSYS2 requires 64 bit Windows 8.1 or higher.

  3. In the wizard, select the required installation folder. Record this directory for later use. The suggested directories are acceptable in most cases. The same applies when you set up a Start menu shortcut step. Once completed, make sure the Run MSYS2 Now box is checked and then select Finish. This will open an MSYS2 terminal window for you.

  4. In this terminal, install the MinGW-w64 toolchain by running the following command:

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
  1. Press Enter to accept the default number of packets in the group. toolchain
    Insert image description here

  2. When prompted whether to continue the installation, please enter. Y

  3. Use the following steps to add the path to the MinGW-w64 folder to the Windows environment variable: binPATH

    1. In the Windows search bar, type "settings" to open Windows Settings.
    2. Search for Edit environment variables for your account.
    3. In User Variables, select the variable and then choose Edit. Path
    4. Select New to add the MinGW-w64 target folder you recorded during installation to the list. If you used the default settings above, this would be the path: C:\msys64\ucrt64\bin
    5. Select OK to save the updated PATH. You need to reopen any console windows to make the new PATH location available.
Check MinGW installation

To check that your MinGW-w64 tools are correctly installed and available, open a new command prompt and type:

gcc --version
g++ --version
gdb --version

You should see output indicating which versions of GCC, g++, and GDB you have installed. If this is not the case:

Make sure your PATH variable entry matches the location of the MinGW-w64 binary where toochain is installed. If the compiler does not exist in the PATH entry, be sure to follow the previous instructions.
If you have correct output but not , you need to install a missing package from the MinGW-w64 toolset. gccgdb
If you receive an "Invalid value for miDebuggerPath" message when compiling, one reason may be that you are missing a package. mingw-w64-gdb

Create a Hello World app

First, let's set up a project.

Start Windows Command Prompt (type Windows Command Prompt in the Windows search bar), and then
run the following command. These will create an empty folder called "Empty Folder" where you can place all your VS Code projects. There, the next command will create and navigate to a subfolder named . From there, you'll open directly in VS Code. projectshelloworldhelloworld

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

The "code ." command opens VS Code in the current working folder, which becomes the "workspace." Accept the Workspace Trust dialog box by selecting "Yes, I trust the author because this is the folder you created."

As you complete this tutorial, you will see three files created in the workspace folder: .vscode

tasks.json (build instructions)
launch.json (debugger settings)
c_cpp_properties.json (compiler path and IntelliSense settings)< /span> In the File Explorer title bar, select the New File button and name the file. helloworld.cpp
Add source code file

"New file title bar" button

Add hello world source code
Now paste the following source code:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    
    
    vector<string> msg {
    
    "Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
    
    
        cout << word << " ";
    }
    cout << endl;
}

Now press Ctrl+S to save the file. Notice that the file you just added appears in the File Explorer view (Ctrl+Shift+E) of the VS Code sidebar:

file explorer

You can also enable AutoSave to save file changes automatically by selecting File >AutoSave. Details about other views can be found in the VS Code user interface documentation.

Run helloworld.cpp

Remember that C++ extensions use the C++ compiler installed on your computer to build your program. Before trying to run and debug in VS Code, make sure you have completed the "Install the MinGW-w64 toolchain" step. helloworld.cpp

Open it to make it the active file. helloworld.cpp

Press the play button in the upper right corner of the editor.

Select the C/C++:g++.exe build and debug activity file from the list of compilers detected on your system.

C++ debug configuration drop-down list

Only on the first run you will be asked to select a compiler. This compiler will be set as the "default" compiler in the file. helloworld.cpptasks.json

After a successful build, the program's output will be displayed in the integrated terminal.

congratulate! You just ran your first C++ program in VS Code!

Guess you like

Origin blog.csdn.net/zcl369369/article/details/134101024