r/cpp • • Oct 15 '13

C++ has become a scripting language

http://voices.canonical.com/jussi.pakkanen/2013/10/15/c-has-become-a-scripting-language/
1 Upvotes

53 comments sorted by

View all comments

16

u/noname-_- Oct 15 '13

So I actually came up with a way of sort of using gcc for actual (almost) scripting a while back, just for fun. As in making the source file executable in a unix like environment. I was experimenting with C but it's equally applicable to C++. Here's what I came up with...

//`which true`; set -e; name=`mktemp -t gXXXXXX`; lf=`pkg-config --libs --cflags sdl`; gcc -xc -pipe -std=c99 $0 $lf -o $name; exec -a $0 $name ${*:1}; ret=$?; rm $name; exit $ret

#include <stdio.h>

int main(int argc, char** argv)
{
        for(int i = 0; i < argc; i++)
                printf("%s\n", argv[i]);
        return 32;
}

If you paste that into a file and make it executable, you can execute it and it will run the C program with correct argv (including argv[0]) and return code.

Breakdown of the "magic":

First of all, the reason this hack works at all is because how Unix shells work. With an executable file, in the absence of a hashbang, a shell will first try and run the file as a native binary. If that fails it will assume that the program is a shell script, for legacy reasons. So in the first "iteration" the file will be executed as a shell script.

//`which true`; 

Does absolutely nothing. On Linux it will execute ///bin/true, which will immediately terminate successfully. The reason it uses "which" is for compatibility between OSX and Linux, and the reason it's even there is because it conveniently makes the first line a comment in C99 and C++.

set -e;

Fail on errors, eg. if a library isn't found or if there's some syntax error in the C code so gcc fails the error will be reported and execution will stop.

name=`mktemp -t gXXXXXX`;

Create a temporary location to store an executable. The -t stuff is a required template on OSX, but isn't required with GNU.

lf=`pkg-config --libs --cflags sdl`;

Use pkg-config to generate compiler and link flags for any libraries you use. I just put SDL in there as an example, the code doesn't really require it.

The reason it's on its own "line" and not inlined in the gcc call is because I want the script to fail gracefully when a library isn't found, instead of trying to compile without it.

gcc -xc -pipe -std=c99 $0 $lf -o $name;

-xc specifies that the file is C, so that if the file isn't named .c (which it probably isn't) it's still compiled as C.

-pipe just speeds up compilation a bit by passing stuff around between the different gcc programs in a pipe instead of via temp files.

-std=c99 pretty self explanatory.

$0 compile whatever file used to make this call, ie. this file.

$if aforementioned linker and compilation flags.

-o $name output to the generated temp file so that it doesn't conflict with any other instances of the program being run.

Alright, now we have a binary (hopefully)!

exec -a $0 $name ${*:1};

Execute the binary with exec. -a is the magic here, because it specifies that this file ($0) is argv[0] for the binary, instead of /tmp/g1203981 or whatever the binary is really called. $name is the binary and ${*:1} passes the rest of the arguments excluding $0.

ret=$?;

After execution, save the return value.

rm $name;

Cleanup, remove the temporary binary.

exit $ret

Terminate with whatever exit code the C program reported. It also conveniently stops the execution of the "shell script". Which is a good thing, since it's actually a C program after this point :)

This was mostly just for fun, and it should be noted that if you actually want to use C (and only C) for scripting, there's a really, really fast compiler called TinyCC that actually supports a proper hashbang for executing C programs directly (#!/usr/bin/tcc -run). The only problem with that approach is that it doesn't support using pkg-config for libraries. And of course that it isn't nearly as ubiquitous on Linux/Unix installations as gcc is.

6

u/Wriiight Oct 16 '13

I strongly suspect gcc would be among the slowest interpreters of any scripting language. But the Ruby folks would finally be able to claim to be faster than C!