Skip to content

Instantly share code, notes, and snippets.

@ChrisVilches
Last active April 25, 2022 09:37
Show Gist options
  • Save ChrisVilches/5f251851e93e45bc9941aa94ec13973d to your computer and use it in GitHub Desktop.
Save ChrisVilches/5f251851e93e45bc9941aa94ec13973d to your computer and use it in GitHub Desktop.
C++ Runner
#!/bin/bash
# A bash utility for quickly running a simple C++ file using:
# c++ my_file.cpp
#
# Features:
# * stderr output becomes red (use std::cerr or fprintf(stderr, ...) to debug).
# * Caches the compiled file based on the code content (if the file doesn't change,
# it will run the same pre-compiled binary).
#
# Instructions:
# Save as ~/bin/c++ (add to path)
# Speed up compile time when using #include <bits/stdc++.h>
# https://codeforces.com/blog/entry/53909 (precompile the header using the same flags as below),
# note that the compilation can be done in the same folder as the original header (no need to create a copy).
color-stderr()(set -o pipefail;"$@" 2>&1>&3|sed $'s,.*,\e[31m&\e[m,'>&2)3>&1
if [ -z "$1" ]
then
echo "File path missing" >&2
exit 1
fi
BIN_TEMP_LIMIT_FILES=30
BIN_TEMP=".bin-tmp"
COMPILE_FLAGS="-pedantic -O2 -Wall -Wextra -std=c++17"
FILES_IN_BIN_TEMP=$(ls $BIN_TEMP | wc -l)
# Clean temp folder if there are too many files
if [[ $FILES_IN_BIN_TEMP -ge $BIN_TEMP_LIMIT_FILES ]]; then
rm $BIN_TEMP/*
fi
HASH=$(sha1sum $1 | cut -d ' ' -f 1)
OUT_FILE="./$BIN_TEMP/$HASH"
if [ ! -f $OUT_FILE ]
then
mkdir -p $BIN_TEMP
g++ $1 $COMPILE_FLAGS -o $OUT_FILE
fi
if [ $? -eq 0 ]; then
time color-stderr $OUT_FILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment