Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
./xxx

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

share|improve this question
add comment

5 Answers

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.

&>/dev/null

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.

;x="${0%.*}"

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
add comment

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 solution.it 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 xxx.com > /dev/null && echo 'xxx.com up' || echo 'xxx.com down' will ping xxx.com and show xxx.com up if the ping is successful, and show xxx.com 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
add comment

Create a Makefile like this:

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

run: xxx
   ./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
add comment

you can write a script. like:

#!/bin/bash

g++ -o $1 $1.cpp
./$1

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

share|improve this answer
add comment

a simple, generic makefile could do it:

$ cat Makefile  
.SUFFIXES: .o .c  

.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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.