c++ in linux

To compile and run a C++ program in Linux, you can follow these steps:

  1. Open a terminal: You can open a terminal in Linux by either using the terminal application or by pressing Ctrl + Alt + T on your keyboard.

  2. Create a C++ file: Use a text editor (such as Vim, Nano, or Gedit) to create a C++ file with a .cpp extension. For example, you can use the command nano hello.cpp to create and open a new file called hello.cpp using the Nano text editor.

  3. Write your C++ code: Inside the 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;
}
  1. Save the file: After writing your code, save the file and exit the text editor.

  2. Compile the C++ program: In the terminal, use the g++ command to compile the C++ program. For example, you can use the command g++ hello.cpp -o hello to compile the hello.cpp file into an executable named hello.

  3. Run the program: Once the compilation is successful, you can run the program by entering the command ./hello in the terminal. This will execute the compiled program and display the output, which in this case will be "Hello, World!".

Congratulations! You have successfully compiled and run a C++ program in Linux.