What is the command to run the following simple C++ program?
#include <iostream>
using namespace std;
// main () is where program execution begins
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
What is the command to run the following simple C++ program?
#include <iostream>
using namespace std;
// main () is where program execution begins
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
First you need to compile and link your program. Assuming the source code is saved in a file yourprog.cpp
, you can do that using the GNU C++ compiler g++
, for example
g++ -Wall -o yourprog yourprog.cpp
The resulting executable will be called yourprog
and you can then run it using
./yourprog
Here's a way to use make
to build and run your program without requiring any additional setup:
make CXXFLAGS='-Wall -Werror' hello_world && ./hello_world
But assuming you will continue developing, you will want to create a file called Makefile
to streamline things further.
CXXFLAGS = -Wall -Werror
default: build
build: hello_world
run: build
<tab>./hello_world
Then you can build and run your program by typing:
make run
If you just want to see if your program compiles without error, type:
make
Other notes:
<tab>
above should be created using the tab key.-Wall -Werror
. These flags prevent certain obvious programming bugs from being ignored by the compiler. That means less debugging work for the programmer.-s
option with make
. It eliminates (usually) unnecessary verbosity.make
is that it doesn't recompile your program if it doesn't need to. This can be a nice time-saver if the program takes a long time to compile. This is especially useful if your project has more than one source (.cpp) file, since these can be compiled independently -- and even in parallel (simultaneously) with the -j
option.There's a nifty package called binfmtc that allows you to run C++ files as if if they were "scripts" -- not requiring an explicit compilation step.
Once the binfmtc
package is installed, your source file needs a /*BINFMTCXX: ...
header comment (see example) and mark the .cpp
file as executable using chmod +x
.
/*BINFMTCXX: -Wall -Werror
*/
#include <iostream>
using namespace std;
// main () is where program execution begins
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
Setup steps:
$ sudo apt install binfmtc
$ chmod +x ./first-time-to-run-c-program-on-ubuntu-14-04.cpp
Running the "script":
$ ./first-time-to-run-c-program-on-ubuntu-14-04.cpp
Hello World
First, you save it to a file, most likely by pressing ctrl+s, then compile it. To compile it, run the following command:
g++ path/to/that/file.cpp -o path/to/output/file.out
Then just drag the output file to the terminal window to run it, because it is now an executable file.
You need to use g++
because c++ is a compiled language, meaning you need a compiler to compile it into machine code. This results in c++ being a faster language than others.
open a terminal and execute the following
1- g++ -o outfilename.bin source.cpp
2- ./outfilename.bin
that assumes the source file is source.cpp
Open terminal by pressing Ctrl+Alt+t
Install GNU C++ Compiler by running following command:
sudo apt-get install g++
Now compile the cpp source file by running this command:
g++ sourceFile.cpp -o anything
You can now run the file like this:
./anything
sudo apt-get install build-essentials
is probably needed, too. ;-) – Rmano Nov 23 '14 at 20:12make
, and the dependency tree forg++
should pull in the standard libs. Also, it's calledbuild-essential
(singular). – steeldriver Sep 24 '16 at 19:02