Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently I've started to learn cpp language on linux,and now I run a cpp file using following commands.

g++ -o xxx xxx.cpp

Is there a way to make it one line command such as compile-and-run xxx.cpp?Thanks.

share|improve this question
up vote 6 down vote accepted

Jeremy Banks has posted one of the most awesome tricks I've seen in quite some time at SO. Basically, he's adding a single Bash-Script line at the top of the cpp file, which allows him to directly compile and execute that file.

//&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";cc -o "$x" "$0")&&exec "$x" "$@"

This explanation is copied from the gist:


Since we don't want this visible in C, we put it in a comment.


Unfortunately // is interpreted as an invalid shell command and produces an error message, so we need to redirect that to /dev/null to get rid of it.


This determines what our compiled filename will be by using string replacement to get rid of any extensions on the current filename.

;[ ! "$x" -ot "$0" ]||

This check if there already is a compiled version newer than the source file. If not...

(rm -f "$x";cc -o "$x" "$0")

...we delete any existing version and then compile our code, using our chosen compiled filename.

&&exec "$x" "$@"

If the compile was successful or unnecessary, pass execution to the compiled code, passing on the arguments given to this script.

This technique is called a Polyglot, which means that it is written in multiple languages and can also be interpreted by those language-interpreters without problems.

share|improve this answer
that's really a nice trick:) – SpawnST Jun 24 '10 at 7:33
the only problem is ,how to add this line to the head automatically everytime I create a cpp file~though this is another topic. – SpawnST Jun 24 '10 at 8:17
@SpawnST: How do you create this files? Most IDEs support adding automatic headers. – Bobby Jun 24 '10 at 9:24
I like this. It is very tricky. – petersohn Jun 24 '10 at 9:31
@Bodbby,i use vim.i'm going to make some hotkey for the code. – SpawnST Jun 24 '10 at 11:06

What about this?
g++ -o xxx xxx.cpp && ./xxx ?
( && means it'll wait until the first task is done, & means it'll start it right after it started the first, you do NOT want this one. )

share|improve this answer
I think Shiki understand well what '&' means in a unix shell, but just for those who are less informed, & means that the first command is run in background (but the output is directed still, to console unless redirection is used.) – bubu Jun 24 '10 at 6:35
Thanks for the works fine.can it be more beautibul by do some alias stuff?I like the style cplandrun xxx.cpp.. – SpawnST Jun 24 '10 at 6:37
@bubu yeah that's right, i got you:) – SpawnST Jun 24 '10 at 6:40
and for the matter, && means that the second command is run only after the first command is run correctly (i.e. return 'ok' to the command) -- if the command is run incorrectly, e.g. syntax error during compilation, '&& ./xxx' will not be run. example: ping -c 1 > /dev/null && echo ' up' || echo ' down' will ping and show up if the ping is successful, and show down if it is not successful. – bubu Jun 24 '10 at 7:02
@SpawnST: An alias wouldn't do it, but as lajuette shows, a script will do it. Or it can be done in a function. Using Shiki's command: crun () { g++ -o $1 $1.cpp && ./$1; }. To use it: crun xxx. – Dennis Williamson Jun 24 '10 at 7:45

Create a Makefile like this:

xxx: xxx.cpp
    g++ -o xxx xxx.cpp

run: xxx

Then just type make run. Of course your Makefile can be much more elaborate than that, it's just a simple example. The important thing is the "run" clause.

share|improve this answer
that's even better than my idea to whip up a script. – lajuette Jun 24 '10 at 6:58
Yes, good solution, and more clean than writing && always. :) – Shiki Jun 24 '10 at 8:01

a simple, generic makefile could do it:

$ cat Makefile  
.SUFFIXES: .o .c  

        cc -o $@ $<  
$ make myprogram  

will compile myprogram.c and will run myprogram. As it is defined with default rules, this works for every .c file in this directory.

Naturally, you must care about libraries, include directories, and other dependencies ...

I hope this helps, nevertheless

share|improve this answer

you can write a script. like:


g++ -o $1 $1.cpp

then make it execupable chmod u+x and call it like yourcode (omit the cpp or alter the script).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .