Skip to content

Instantly share code, notes, and snippets.

@jdarpinian
Last active March 20, 2024 15:28
Show Gist options
  • Star 119 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jdarpinian/1952a58b823222627cc1a8b83a7aa4e2 to your computer and use it in GitHub Desktop.
Save jdarpinian/1952a58b823222627cc1a8b83a7aa4e2 to your computer and use it in GitHub Desktop.
Add one line to your C/C++ source to make it executable.
///bin/true;COMPILER_OPTIONS="-g -Wall -Wextra --std=c99 -O1 -fsanitize=address,undefined";THIS_FILE="$(cd "$(dirname "$0")"; pwd -P)/$(basename "$0")";OUT_FILE="/tmp/build-cache/$THIS_FILE";mkdir -p "$(dirname "$OUT_FILE")";test "$THIS_FILE" -ot "$OUT_FILE" || $(which clang || which gcc) $COMPILER_OPTIONS -xc "$THIS_FILE" -o "$OUT_FILE" || exit;exec "$OUT_FILE" "$@"
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
///bin/true;COMPILER_OPTIONS="-g -Wall -Wextra --std=c++14 -O1 -fsanitize=address,undefined";THIS_FILE="$(cd "$(dirname "$0")"; pwd -P)/$(basename "$0")";OUT_FILE="/tmp/build-cache/$THIS_FILE";mkdir -p "$(dirname "$OUT_FILE")";test "$THIS_FILE" -ot "$OUT_FILE" || $(which clang++ || which g++) -xc++ $COMPILER_OPTIONS "$THIS_FILE" -o "$OUT_FILE" || exit;exec "$OUT_FILE" "$@"
#include <iostream>
int main() {
std::cerr << "Hello, world!" << std::endl;
return 0;
}
#!/bin/bash
///bin/true;COMPILER_OPTIONS="-g -Wall -Wextra --std=c99 -O1 -fsanitize=address,undefined";THIS_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)/$(basename "${BASH_SOURCE[0]}")";OUT_FILE="/tmp/build-cache/$THIS_FILE";mkdir -p "$(dirname "$OUT_FILE")";test "$THIS_FILE" -ot "$OUT_FILE" || tail -c +12 "$0" | $(which clang || which gcc) -xc $COMPILER_OPTIONS -o "$OUT_FILE" - || exit $?;exec -a "$0" "$OUT_FILE" "$@"
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
#!/bin/bash
///bin/true;COMPILER_OPTIONS="-g -Wall -Wextra --std=c++14 -O1 -fsanitize=address,undefined";THIS_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)/$(basename "${BASH_SOURCE[0]}")";OUT_FILE="/tmp/build-cache/$THIS_FILE";mkdir -p "$(dirname "$OUT_FILE")";test "$THIS_FILE" -ot "$OUT_FILE" || tail -c +12 "$0" | $(which clang++ || which g++) -xc++ $COMPILER_OPTIONS -o "$OUT_FILE" - || exit $?;exec -a "$0" "$OUT_FILE" "$@"
#include <iostream>
int main() {
std::cerr << "Hello, world!" << std::endl;
return 0;
}
@bontchev
Copy link

bontchev commented Feb 7, 2018

gcc does not like the option -fsanitize=address,undefined on my machine; I had to delete that part, in order to make it work.

@kevien
Copy link

kevien commented Feb 8, 2018

what's apply scenarios?

@compilelife
Copy link

nice hack, and fun, but just fun

@cedriczirtacic
Copy link

And Perl?

#ifndef C
$_=$0,s/\.c$//,print`gcc -DC -o $_ $0 && ./$_; rm -f $_`;
__END__
#endif
#include <stdio.h>

int main() {
    puts("Hello World!");
    return 0;
}

@torarnv
Copy link

torarnv commented Aug 8, 2018

//usr/bin/env clang++ $0 -o ${o=`mktemp`} && exec $o $*
#include <stdio.h>
int main() {
    printf("Hello World!\n");
    return 0;
}

@scentoni
Copy link

Due to an issue described here there is a warning printed on macOS that can be remedied using

///$(which true);export MallocNanoZone=0;COMPILER_OPTIONS="-g -Wall -Wextra --std=c++17 -O1 -fsanitize=address,undefined";THIS_FILE="$(cd "$(dirname "$0")"; pwd -P)/$(basename "$0")";OUT_FILE="/tmp/build-cache/$THIS_FILE";mkdir -p "$(dirname "$OUT_FILE")";test "$THIS_FILE" -ot "$OUT_FILE" ||$(which clang++ || which g++) -xc++ $COMPILER_OPTIONS "$THIS_FILE" -o "$OUT_FILE" || exit;exec "$OUT_FILE" "$@"
#include <iostream>

int main() {
  std::cerr << "Hello, world!" << std::endl;
  return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment