terminal compile c++

To compile a C++ program using the terminal, follow these steps:

  1. Open the terminal on your computer. This can usually be done by searching for "terminal" in your operating system's search bar or by navigating to the appropriate application in your system's utilities folder.

  2. Navigate to the directory where your C++ program is located. Use the "cd" command followed by the directory path to change your current working directory to the location of your program. For example, if your program is located in the "Documents" folder, you would use the command "cd Documents" to navigate to that folder.

  3. Once you are in the correct directory, use the "g++" command followed by the name of your C++ file to compile it. For example, if your file is named "program.cpp", you would use the command "g++ program.cpp" to compile it.

  4. If there are no errors in your code, the compilation process will generate an executable file with a default name "a.out". You can run this file by typing "./a.out" in the terminal and pressing enter.

  5. If you want to specify a custom name for the output executable file, you can use the "-o" flag followed by the desired name. For example, if you want the output file to be named "myprogram", you would use the command "g++ program.cpp -o myprogram" to compile it.

  6. After successful compilation, you can run the executable file by typing "./myprogram" (or the name you specified) in the terminal and pressing enter.

That's it! Your C++ program should now be compiled and running.