C++ configuration in VSCODE under ubuntu

Implement function

In this article, you will learn how to configure the GCC/g++ compiler and GBD debugger on the VS Code IDE on a Linux system. After configuring VS Code, you can compile and debug a simple C++ program on VS Code.

Prerequisites required

  1. Install Visual Studio Code;
  2. Install the C/C++ extension component on VS Code, search for "C/C++" in the extension of VS Code, and install it;
  3. Make sure the GCC compiler is installed on your Linux system. You can check it through the gcc -v shell command. If GCC is not installed on your Linux system, you can use the sudo apt-get updateshell command to update the system package list and its dependencies. ;
  4. To install the GDB debugger, you can use the sudo apt-get install build-essential gdb command.

Maybe the above method is not detailed enough. You can search for solutions online for specific situations. Here we mainly introduce the configuration.

a simple example

We create a new directory named projects, and create a helloword directory under the directory. Then, use VS Code to open the helloworld directory and create a new helloworld.cpp file.

We add the following code in helloworld.cpp:

#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;
}

Compile helloworld.cpp, click the button in the upper right corner to run, select the first one, and then the first compilation will be completed, and it .vscodewill be generated in the file task.json. Run it again.
Insert image description here
The output results are as follows:
Insert image description here

debug

Just debug and run it directly
Insert image description here

Comment

This article refers to https://zhuanlan.zhihu.com/p/344940452

If you have an old version of the c++ plug-in, you can refer to the above document to install it. If it is a new version, you can use the installation method in this article. Compared with the old version, the new version is simpler and more efficient.

Guess you like

Origin blog.csdn.net/frighting_ing/article/details/129231405