i have a text file on my desktop saved as a .cpp file and has code in it.
how do i compile and run it in in terminal using the given g++????

try

g++ yourfile.cpp

This will produce a binary called a.out. You can also do this to specify the name of the binary file:

g++ yourfile.cpp -o nameOfBinary

"nameofbinary" is absolutely anything that you care to type. e.g. image we are making a program that I want to call "sumNumbers". You have written some c++ in a file called firstProgram.cpp and you want the output to be called sumNumbers. you would do this

g++ firstProgram.cpp -o sumNumbers

Once that had successfully completed. [If it doesn't then you wil need to edit firstProgram.cpp and fix the problem.] You can run it with

./sumNumbers

Note the ./ That is because you have to have a path in Linux to the executable.
Executable is a file that is to be run, e.g. the output of a compiler. Path is the places that executables can be found. The variable PATH contains all the default places that commands exist, e.g. /usr/bin in which you will find g++ and ls etc.
However, you have created the file sumNumbers in a directory that is unlikely to be in the path. Therefore you have to explicitly set the path in the executable line which you do by adding ./ or if you which the whole path e.g. /home/coding101/test/sumNumbers. Note this assumes your login name is coding101, but just replace that with your login name or, you can use the ~ symbol which just means home directory, e.g. ~/test/sumNumbers

Edited 6 Years Ago by StuXYZ: n/a

This article has been dead for over six months. Start a new discussion instead.