Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Build and run a C++ file, showing execution time and exit code.
- # Usage: cpprun srcfile [arg2] ... [arg99] [<file.in] [>file.out]
- # Example: cpprun code.cpp <file.in >file.out (specifying .cpp is optional)
- cpprun() {
- basepath="${1%%.*}" # Strip extension.
- [[ "$1" == "$basepath" ]] && srcpath="$basepath".cpp || srcpath=$1
- # Change the build command and arguments below as you like it it.
- # On Mac, `gcc` is likely just a clang shim and won't have bits/stdc++.h.
- # Install the real GCC with `brew install gcc` and invoke with `g++-13`.
- g++-13 "$srcpath" -o "$basepath" -O3 -std=gnu++17 -Wl,-stack_size -Wl,20000000 -Wall -Wno-variadic-macros
- status=$?
- if [ $status -eq 0 ]; then
- # On Mac, must run `brew install coreutils`
- [[ "$(uname)" == "Darwin" ]] && start=$(gdate +%s.%N) || start=$(date +%s.%N)
- # Check if argument is a path, else run in current directory.
- [[ "$basepath" == *\/* ]] && "$basepath" "${@:2:99}" || ./"$basepath" "${@:2:99}"
- status=$?
- [[ "$(uname)" == "Darwin" ]] && end=$(gdate +%s.%N) || end=$(date +%s.%N)
- runtime=$(python -c "print('{0:.0f}m {1:.3f}s'.format(*divmod(${end}-${start},60)))")
- echo "Finished in $runtime with exit code ${status}." >&2
- return $status
- else
- echo "Build failed" >&2
- return $status
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement