Last active
December 25, 2015 16:59
-
-
Save panzi/7010132 to your computer and use it in GitHub Desktop.
Print some C++ expresions. -iFILE to include FILE, -I -L -l -E -D -S and -O will be passed as such to g++. More compiler flags can be passed via -f$FLAG or $CXXFLAGS environment variable. Example: cppexpr.sh -f-std=c++0x -imath.h -lm 'log2(8)'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
incs="" | |
flags="$CXXFLAGS" | |
only_preproc=off | |
no_asm=off | |
no_space=off | |
if [ "$CXX" = "" ]; then | |
CXX=g++ | |
fi | |
while getopts i:I:L:l:W:O:D:SEnf: opt "$@"; do | |
case $opt in | |
I|L|l|W|O|D) | |
flags="$flags -$opt$OPTARG" | |
;; | |
f) | |
flags="$flags $OPTARG" | |
;; | |
S) | |
no_asm=on | |
;; | |
E) | |
only_preproc=on | |
;; | |
n) | |
no_space=on | |
;; | |
i) | |
incs="$incs | |
#include \"$OPTARG\"" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
exprs="" | |
if [ $no_space = on ]; then | |
for ex in "$@"; do | |
exprs="$exprs<< ($ex) " | |
done | |
else | |
for ex in "$@"; do | |
exprs="$exprs<< ($ex) << ' ' " | |
done | |
fi | |
args="-x c++ -Wall -Werror $flags -" | |
{ | |
cat << EOF | |
#include <iostream> | |
#include <iomanip> | |
#include <cmath> | |
$incs | |
using namespace std; | |
int main (int argc, char *argv[]) { | |
std::cout $exprs<< std::endl; | |
return 0; | |
} | |
EOF | |
} | if [ "$only_preproc" = on ]; then | |
"$CXX" $args -E -o - | |
elif [ "$no_asm" = on ]; then | |
"$CXX" $args -S -o - | |
else | |
tmp=/tmp/cppexpr.$$.out | |
"$CXX" $args -o $tmp - && { | |
$tmp | |
code=$? | |
rm -f $tmp | |
exit $code | |
} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment