Skip to content

Instantly share code, notes, and snippets.

@akiross
Last active April 26, 2020 14:39
Show Gist options
  • Save akiross/8381ec28b46651492e8a5b374667be0d to your computer and use it in GitHub Desktop.
Save akiross/8381ec28b46651492e8a5b374667be0d to your computer and use it in GitHub Desktop.
Compile a C/C++ program without make, but using a simple header that is valid in both C/C++ and (ba)sh.
#if 0
# Run this file with bash to compile the C++ program.
CC=g++
FLAGS=
# Output file will have the same name of this file without extension
OUT_FILE=${BASH_SOURCE%.cpp}
# Ensure we are not overwriting
if [ -f "$OUT_FILE" ]; then
read -n 1 -p "File exists, overwrite? [y/N]" yn
case $yn in
[Yn]* ) echo;;
* ) exit;;
esac
fi
# Compile
echo "Compiling to $OUT_FILE"
$CC $BASH_SOURCE -o $OUT_FILE $FLAGS
exit
#endif
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment