top of page

Ask a Question

Public·6 members

How to run .cpp from Terminal



Compile and Run C++ in Terminal
How to Compile and Run C++ in terminal

1) if you have single file Only “main.cpp”


1. 1 compile and run c++ without giving target name

How to compile:

g++ main.cpp

one a.out file will be generated by default


How to Run

./a.out

1. 2 compile and run c++ with target name


How to compile:

g++ main.cpp -o test

one test.out file will be generated by default


How to Run

./test


2) Multiple .cpp files:


suppose we three files

main.cpp

chess.cpp

chess.h

How to compile multiple files in c++

g++ main.cpp chess.cpp -o output 

Here we have three file but we need to compile only chess.cpp file not header file


How to run

./output

3) All .cpp files


g++ *.cpp -o outputname

./outputname

4) Advanced setting to run c++

If you want to include standard while compiling then you can do with following ways


Compiling c++ with -std=c++11

g++ -std=c++11 main.cpp chess.cpp -o output 

How to run

./output 

5) Compile CPP make file


Make filename # creates “main” application 
./main #Run application on terminal

6) C++ Compilation with activates all Warnings

”-Wall” activates all Warnings

g++ -Wall main.cpp -o output

Run

./output
826 Views
bottom of page