11 September 2012 update
The last revision of Cling, r45925, doesn’t need any modifications in order to compile on Cygwin, I’ve removed some outdated instructions about manually editing the libcling.exports file.
3 September 2012 update
After a discussion with the main developer of Cling I’ve removed a paragraph about adding some hard codded paths in the Clang sources, this was an unnecessary complication. I’ll keep updating this article if we’ll find any other simplifications.
In my last article I wrote about Cling, the C and C++ interpreter. If you want to have a taste of what Cling can do for you, read my earlier post or this one. In short, Cling is a C++ REPL that uses Clang and LLVM for just in time compilation and parsing of your code. Currently, Cling can be officially compiled only on Unix like operating systems (e.g. Linux or OSX). What if you can’t, for various reasons, use a Unix like OS ? Well, this post is for you, the Windows user!
In principle, you could use Visual Studio to compile Cling, however because of the incompatibilities between Clang and Visual Studio, the resulting interpreter will let you use only C code. At the time of this writing, a Visual Studio compiled Clang is not able to build C++ executables … A C only interpreter, while useful, is not so attractive, at least not for me.
The solution is to use Cygwin, which is a collection of tools which provide a Linux look and feel environment for Windows. From the Cygwin main page download setup.exe, this will install Cygwin on your C drive. During the installation phase be sure to select the next group of packages: binutils, flex, bison, m4, subversion, python (use the Cygwin version !), clang, llvm, ncurses, make, patch, gcc4 (gcc4-core, gcc4-gfortran, gcc4-g++), perl, autoconf, automake, libtool, libxml2. At the end of this step you should have a Cygwin Terminal icon on your Desktop, from which you will be able to launch Cygwin.
Next step is to download the sources for LLVM, Clang and Cling. It is important to note at this point that LLVM is currently a moving target, so we need to download the last good LLVM revision, currently this is *163370 *. You should check yourself this number each time you do a fresh Cling compilation and update the next instructions accordingly.
Let’s start by double clinking the Cygwin Terminal. In the Terminal window that is presented to you, paste the next instructions one by one:
1 2 3 4 5 6 7 8
mkdir Cling && cd Cling svn co -r 163370 http://llvm.org/svn/llvm-project/llvm/trunk llvm cd llvm/tools svn co -r 163370 http://llvm.org/svn/llvm-project/cfe/trunk clang svn co -r 45925 http://root.cern.ch/svn/root/trunk/interpreter/cling/ cd .. cat tools/cling/patches/*.diff | patch -p0 cd ..
Now, we are ready to do the actual compilation of Cling. In the Cygwin Terminal paste these lines one by one (depending on the speed of your PC this phase could take from half an hour to a few hours):
1 2 3 4
mkdir build && cd build ../llvm/configure --prefix=/usr/cling --enable-optimized --enable-targets=host make -j 4 make install
If you’ve seen no error message during the above phase, great! Now you have Cling compiled on your machine. Each time you want to use the interpreter, open a Cygwin Terminal and paste the next line:
now, write in the Terminal cling and the interpreter should start:
1 2 3 4 5 6 7
$ cling ****************** CLING ****************** * Type C++ code and press enter to run it * * Type .q to exit * ******************************************* [cling]$
A short example of how you can use the interpreter (for more examples read my previous article):
1 2 3 4 5 6
[cling]$ int a (int) 0 [cling]$ int b = 10; [cling]$ a + b (int const) 10 [cling]$
If you are interested in learning more about the new C++11 syntax I would recommend reading Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:
or, if you are a C++ beginner you could read C++ Primer (5th Edition) by S. B. Lippman, J. Lajoie, B. E. Moo.blog comments powered by Disqus