c++ in linux
To compile and run a C++ program in Linux, you can follow these steps:
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.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 commandnano hello.cpp
to create and open a new file calledhello.cpp
using the Nano text editor.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;
}
Save the file: After writing your code, save the file and exit the text editor.
Compile the C++ program: In the terminal, use the
g++
command to compile the C++ program. For example, you can use the commandg++ hello.cpp -o hello
to compile thehello.cpp
file into an executable namedhello
.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.