I am new to Linux. I am using Ubuntu 11.04 and do not know how to compile and execute C++ program in it. I need to know the commands to Compile and Execute a C++ program in Linux.
To compile your c++ code, use:
foo.c in the example is the name of the program to be compiled.
This will produce an executable in the same directory called
g++ should already be in your $PATH, so you don't need to call
As noted by @con-f-use, the compiler will usually make this file executable, but if not, you can do this yourself (so the command to execute,
To specify the name of the compiled output file, so that it is not named
This will compile
You need g++,for gcc may can't compile cpp file easily.
I'm making two assumptions here:
The simplest way to compile a C++ program on Ubuntu, or any other Linux distro for that matter, is to type
The above commands assume you are already in the location of the source files, but both the source file and target output file may also be specified as a directory. For example
will compile a C++ source file located on your desktop and place the executable binary in a
This will generate output file named as (e.g.):
If you create another program and compile that (e.g.):
This will also generate output file named as (e.g):
This means you can get the output of only last compiled program if you execute that. To avoid this use (e.g.)
now, your output file will be :
Now, you can execute the file with the name specified by you.
protected by A.B. Jul 11 at 8:52
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?