Building Cling (the C++ interpreter) on Windows
Posted on September 2, 2012 by Sol
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 mkdir Cling && cd Cling 2 svn co -r 163370 http://llvm.org/svn/llvm-project/llvm/trunk llvm 3 cd llvm/tools 4 svn co -r 163370 http://llvm.org/svn/llvm-project/cfe/trunk clang 5 svn co -r 45925 http://root.cern.ch/svn/root/trunk/interpreter/cling/ 6 cd .. 7 cat tools/cling/patches/*.diff | patch -p0 8 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 mkdir build && cd build 2 ../llvm/configure --prefix=/usr/cling --enable-optimized --enable-targets=host 3 make -j 4 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:
1 export PATH=/usr/cling/bin:$PATH
now, write in the Terminal cling and the interpreter should start:
1 $ cling 2 3 ****************** CLING ****************** 4 * Type C++ code and press enter to run it * 5 * Type .q to exit * 6 ******************************************* 7 [cling]$
A short example of how you can use the interpreter (for more examples read my previous article):
1 [cling]$ int a 2 (int) 0 3 [cling]$ int b = 10; 4 [cling]$ a + b 5 (int const) 10 6 [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.