Last active
May 31, 2021 18:34
-
-
Save charmoniumQ/729ea258e1bcdc13476c89a6683c9206 to your computer and use it in GitHub Desktop.
Executable C source
This file contains hidden or 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
#define C_AND_POSIX_SHELL_POLYGLOT /* | |
# This section gets interpreted by the POSIX shell. | |
# It is inside a C comment, so it gets ignored by the C compiler. | |
CFLAGS= | |
NIXPKGS= | |
TARGET=/tmp/a.out | |
exec nix-shell \ | |
--pure \ | |
--packages clang_12 ${NIXPKGS} \ | |
--command "echo Compiling && clang++ ${CFLAGS} ${0} -o ${TARGET} && echo Running && ${TARGET} ${@}" | |
# The exec ensures shell does not interpret beyond this point. | |
*/ | |
#include <iostream> | |
int main() { | |
std::cout << "Hello, world!" << std::endl; | |
} | |
/* | |
This is a self-contained C source file: If you run this at the shell, | |
and it will find its own dependencies (through Nix), compile itself, | |
and run itself. | |
Usage: | |
$ sudo apt install -y nix | |
$ chmod +x test.cpp | |
$ ./test.cpp | |
Compiling | |
Running | |
Hello, world! | |
See: https://gist.github.com/charmoniumQ/729ea258e1bcdc13476c89a6683c9206 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment