how to run cpp using gcc vscode
To run C++ code using GCC in Visual Studio Code (VSCode), you can follow these steps:
Install GCC: First, ensure that you have GCC (GNU Compiler Collection) installed on your system. GCC is a compiler for C, C++, and other programming languages. You can install GCC by following the instructions specific to your operating system.
Install VSCode: If you haven't already, download and install Visual Studio Code from the official website (https://code.visualstudio.com/). VSCode is a popular source code editor that provides a wide range of features and extensions.
Open VSCode: Once you have installed VSCode, open it on your system.
Create a new C++ file: In VSCode, open a new file by clicking on "File" in the menu bar, selecting "New File," and saving it with a
.cpp
extension (e.g.,example.cpp
).Write your C++ code: In the newly created file, write your C++ code. For example, you can write a simple "Hello, World!" program:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Open the integrated terminal: To open the integrated terminal in VSCode, click on "View" in the menu bar, select "Terminal," and then click on "New Terminal." This will open a terminal window at the bottom of the VSCode interface.
Compile the C++ code: In the terminal, use the
g++
command followed by the name of your C++ file to compile it. For example, if your file is namedexample.cpp
, you can use the following command:
g++ example.cpp -o example
This command tells the GCC compiler (g++
) to compile example.cpp
and produce an output file named example
. The -o
flag is used to specify the output file name.
- Run the compiled program: After successfully compiling the code, you can run the compiled program by entering its name in the terminal. For example, if your output file is named
example
, use the following command:
./example
This command runs the compiled program and displays the output on the terminal.
That's it! By following these steps, you can run C++ code using GCC in Visual Studio Code.