5 ms·
Cling: Running C++ in an interpreter
- shadyabhi 14y agoSo, what are it's limitations, if any?
- coldgrnd 14y agoI'd say error recovery could be better. You often get kicked out of the session and than your environment is lost. Also: they claim the 'auto' keyword is implicit, does not seem to work for me. But it's pretty cool to use!
- codedivine 14y agoWell, for anyone interested in Cling, check out this Google tech talk: http://www.youtube.com/watch?v=f9Xfh8pv3Fs http://www.youtube.com/watch?v=f9Xfh8pv3Fs This is a CERN project and it uses Clang from the LLVM project. The idea is simple: use the clang to generate LLVM and then use LLVM just-in-time compiler.
- malkia 14y agoBefore that CINT was used (another C++ interpretter) - here is the page where they announce the future transition CINT -> CLING - http://root.cern.ch/drupal/content/cling http://root.cern.ch/drupal/content/cling
- zenogaisis 14y agoWhy would I do that :S
- jeremiep 14y agoWhy not? It's great to quickly test C++, I have yet to find a developer who doesn't love a REPL for one. C++ could also be used as an embedded extension language using something like this.
- pmr_ 14y agoI share your enthusiasm but there are limits. C++ compilation speed (and thus interpretation speed) will make it quite impossible to use it as an extension language. And while I like and use C++ a lot I don't think it is the right language for this purpose. It can do quite a lot of things, but working in a homogeneous language in your extensions as well as your core product doesn't have enough benefits compared to using a systems programming language and an extension language.
- jeremiep 14y agoWell if it's possible for C++, it's definitely possible for D which is benchmarked to compile 100 times faster and has close-to-ruby's productivity in a close-to-metal language like C. An added benefit is that D already has most of the C++11 features and more. I would very much like to use D as an extension language.
- charliesome 14y agoD has `rdmd`
- Arelius 14y ago> C++ compilation speed (and thus interpretation speed) will make it quite impossible to use it as an extension language. I think you vastly underestimate the speed of the clang C++ compiler (or any modern C++ compiler at that). I can't imagine that the compilation time for anything that could be classified as an "extension" would be significant in any meaningful way.
- pmr_ 14y agoclang doesn't outperform gcc on most projects I work on or that speed increase is not significant. I run quite a lot of test-suites - maybe I should try to measure them and provide some real numbers for reference instead of hand-wave myself through this argument. If I where to provide extensions points to my C++ project they would certainly contain templates and that would also mean that they would require significant compilation time (compared to what you would expect). Even if I didn't, a single header that pulls in a huge preprocessor library could ruin speed. I'm inclined to believe you if we are talking about a Qt-style C++, but that is only a subset of possible code. I would be happy to be proven wrong, too.
- dbattaglia 14y agoI can see it being helpful if you are working in C++ all day and need to try small things out. I use LinqPad for doing the same w/ C#, helps when you need to test out some smaller ideas w/o all the cruft of projects and references / includes / etc.
- Mon_Ouie 14y agoAlthough getting an interactive environment definitely is a nice thing, I'd argue you don't have to create a whole project directory, etc. to play with an idea; I usually write it in a single file.
- deckiedan 14y agoCould you not just use a project directory template? All you need really is a .c(xx|pp|whatever) file, a Makefile, and then the workflow for a new idea is: cd ~/src; cp -R c-idea-template foobar; cd foobar; $EDITOR test.c* and in your editor (say vim) just run :make or write a .sh file with all of the above in it so it's just one step. No complex install procedure, you get all your normal tools and stuff. Alternaitvely, create a 'test projects' git(hub) project, with the files you want in it, and create a new branch for each idea. That way you get backups as well.
- coldgrnd 14y ago@fferen, @jlarocco, @Mon_Ouie, @deckiedan: I know you can probably set up all you need within the blink of a second (using emacs or vim or a template setup). Actually I'm so hooked on this idea I even wrote my own rake based C/C++ Buildsystem (https://github.com/marcmo/cxxproject https://github.com/marcmo/cxxproject). But still sometimes I prefer not having to set up anything and not having to clean up anything after I end my experiments. ... Idea - drop into REPL and try out - exit - done.
- fferen 14y agoI just keep a fixed .cpp file with a bunch of common includes and directives, and alias the g++ command to link to common libraries, so my workflow goes: > vim temp.cpp > g++ temp.cpp > ./a.out Very similar to my process with Python, actually. Every time I use the REPL for some experimenting, the code ends up outgrowing it and I have to stuff it in a file anyway, so I may as well cut out the middleman to begin with. YMMV.
- sanxiyn 14y agoI too put the code in a file even in Python, but REPL is still valuable. I use "python -i" which runs your code (defines functions, classes, etc.) and then stop, and put you in that environment. This is very useful. It seems Cling could be used the same way: .x command.
- voltagex_ 14y agoI must be reading the wrong Python tutorials. Thank you for this
- rcfox 14y agoIf you don't need any special compiler flags, it's even simpler: make temp ./temp
- jlarocco 14y agoIt's a cool idea, but his reason for creating it is kinda dumb. Creating an entire "project" just to check a code snippet is just silly. Just create a "testing" directory and throw your one off test files into it and compile/run them there. I start mine with a comment explaining what I'm testing, why I'm testing it, and what special compilation flags are required, if any. I even have an Emacs macro that fills in the boilerplate includes and main function. The overhead involved is probably less than 15 seconds. It has the advantages that I can test multiple compilers and I keep a history of the things I've tried.
- geofft 14y agoThe ROOT project has been using CINT for ages. Talk to your favorite physicist friend -- they'll be confused why the CS crowd _doesn't_ have this technology. This is just an updating of what must be an awful hack to be built on LLVM infrastructure.
- Create 14y agoBecause actually using ROOT is painful, and the CS crowd had lisp and now python/pypy. http://www.insectnation.org/howto/living-without-root http://www.insectnation.org/howto/living-without-root https://linuxfr.org/nodes/18919/comments/632920 https://linuxfr.org/nodes/18919/comments/632920 http://comments.gmane.org/gmane.comp.lang.c%2B%2B.root/5924 http://comments.gmane.org/gmane.comp.lang.c%2B%2B.root/5924
- joebo 14y agoI've used libtcc from tcc to do something similar on a prototype listening on a socket to do queries over a 2 gig memory mapped file. I'd pass over the query as a string of C that would be dynamically compiled and executed by libtcc. It worked really well but ultimately didn't go anywhere other than research. Here's an example from the distribution (first google result for the file): http://www.koders.com/c/fidC76C8B834DFF05F1D0BD61220AC19E24685698A9.aspx http://www.koders.com/c/fidC76C8B834DFF05F1D0BD61220AC19E246.... TCC can be found here: http://bellard.org/tcc/ http://bellard.org/tcc/
- sedachv 14y agoI am surprised more projects don't use TCC for that. It's an awesome little compiler and using it that way saves way more time over writing DSLs. Even projects that have to compile C all the time (like Lisp->C compilers such as ECL and Gambit Scheme) use GCC, which is stupid slow.
- freepipi 14y agoI think it is very useful if you want to know more about c++,especially some feature you 're not very sure about. interactive interpret make it very intuitive, and it will save you time,because you don't need to compile the code. but one limit is that now it doesn't support template , you still have to write a source file if you want to use template.
- mgurlitz 14y agoTo be clear, those empty #include's are typos, not Cling inferring desired header files. Both are: #include <iostream>
- coldgrnd 14y agoUuhhh...damn! I messed that up. corrected now! thanks!
- sedachv 14y agoTo provide more precedents and a little history: The first C "interpreters" I know of were for Lisp machines: Symbolics' C compiler (http://www.bitsavers.org/pdf/symbolics/software/genera_8/User_s_Guide_to_Symbolics_C.pdf http://www.bitsavers.org/pdf/symbolics/software/genera_8/Use...) and Scott Burson's (hn user ScottBurson) ZetaC for TI Explorers/LMIs and Symbolics 3600s (now available under the public domain: http://www.bitsavers.org/bits/TI/Explorer/zeta-c/ http://www.bitsavers.org/bits/TI/Explorer/zeta-c/). Neither of them are interpreters, just "interactive" compilers like Lisp ones are. I am writing a C to Common Lisp translator right now (https://github.com/vsedach/Vacietis https://github.com/vsedach/Vacietis). This is surprisingly easy because C is largely a small subset of Common Lisp. Pointers are trivial to implement with closures (Oleg explains how: http://okmij.org/ftp/Scheme/pointer-as-closure.txt http://okmij.org/ftp/Scheme/pointer-as-closure.txt but I discovered the technique independently around 2004). The only problem is how to deal with casting arrays of integers (or whatever) to arrays of bytes. But that's a problem for portable C software anyway. I think I'll also need a little source fudging magic for setjmp/longjmp. Otherwise the project is now where you can compile-file/load a C file just like you do a Lisp file by setting the readtable. There's a few things I need to finish with #includes, enums, stdlib and the variable-length struct hack, but that should be done in the next few weeks. This should also extend to "compiling" C to other languages like JavaScript, without having to go through the whole "emulate LLVM or MIPS" garbage that other projects like that do. I think I figured out how to do gotos in JavaScript by using a trampoline with local CPS-rewriting, which is IMO the largest challenge for an interoperable C->JS translator. As to how to do this for C++, don't ask me. According to the CERN people, CINT has "slightly less than 400,000 lines of code." (http://root.cern.ch/drupal/content/cint http://root.cern.ch/drupal/content/cint). What a joke.
- gsnedders 14y agoEmscripten in no way emulates LLVM (as far as I know it's the only C-to-JS compiler that uses LLVM, and hence must be what you're referring to!) — it compiles LLVM to JS, with no emulation, and there's currently work going on to re-implement it as a LLVM backend (it currently is an entirely separate codebase that takes LLVM bitcode in and spits JS out).
- tree_of_item 14y agoThe LLVM infrastructure gets more amazing every day; Emscripten and Cling are very exciting projects. C++ gets a lot of flak but it's still got a huge amount of life in it.
- clobber 14y agoFor small programs, something like CodeRunner is nice too: http://krillapps.com/coderunner/ http://krillapps.com/coderunner/
- mlvljr 14y agoHas anyone mentioned CH already (http://www.softintegration.com/ http://www.softintegration.com/)? Kind of an interesting (hi-level) tool, too.
- solenskiner 14y agoCould this be used to make a c/c++ plugin for lightroom, once it is out and stable?