
Show HN: Cpi – Immediately compile and execute c++ program like a scripting lang - kazzn
http://treefrogframework.github.io/cpi/
======
paulproteus
This is great! Nice to see another project in the history of C/C++
interpreters including:

\- cling: [https://root.cern.ch/cling](https://root.cern.ch/cling)

\- cint:
[http://www.hanno.jp/gotom/Cint.html](http://www.hanno.jp/gotom/Cint.html)

\- Ch: [https://www.softintegration.com/](https://www.softintegration.com/)

If people know of other ones, I'd love to hear about them in the comments!

~~~
gravypod
From what I remember from watching Terry's videos he also did something like
this in TempleOS with his compiler HolyC. His shell is apperently just his
compiler running in an interactive mode.

HolyC is "kind of" C from a compiler perspective so I think it counts.

------
kazinator
This is a task on Rosetta:

[https://rosettacode.org/wiki/Native_shebang](https://rosettacode.org/wiki/Native_shebang)

Also:

[https://rosettacode.org/wiki/Multiline_shebang](https://rosettacode.org/wiki/Multiline_shebang)

I contributed the C one here.

------
psykotic
Rather than using shebang notation for running C source files, which as noted
leads to code which isn't legal input to a normal C compiler, there is a
standard trick to please both the shell and the C compiler:

    
    
        #if 0
        /bin/env cpi "$0"
        exit
        #endif
    

And you don't need this cpi program depending on your tolerance for hacks:

    
    
        #if 0
        out=/tmp/$RANDOM
        gcc "$0" -o $out
        $out
        rm $out
        exit
        #endif

~~~
jjnoakes
This only works if invoked by the shell, I believe. If you exec that file
directly it will fail unless I'm mistaken (can't test at the moment).

~~~
psykotic
Yeah, I should have mentioned that. Whereas shebang notation is understood by
the executable loader, not the shell.

------
hellofunk
I do something very similar to this, but just using cmake and basic bash
scripts to launch the compilation and then run the executable all at once. And
for the interpreter mode, a bash script can just automatically dump that into
a main function, and then compile and run it all at once.

It's cool, but you can set something up like this just a few lines of bash
code.

------
trelliscoded
The page claims that CPI is an interpreter, but as this post says it is not.
It just uses a regular compiler to build the input, then fiddled with the file
descriptors to hook up the new executable's I/O to cpi's existing fds.

------
tonmoy
If nothing else, the interactive mode is going to be a life saver for me.

