The “4C” Development Environment

A few weeks ago, I decided to start a new project named Fix. Today I’ll give a short overview of the installation of the “4C” development environment I chose.

The Development Environment

Since I wanted to learn and play with new stuff, I decided to build a development environment out of stuff I have not used before.

I had been developing under Windows for what feels like an eternity. This has changed last year when I started at Zühlke. In the current assignment, I mostly work on a Xubuntu 16.04 virtual machine. I had not known how much I had missed a good shell!

So I use a similar VM for the development of Fix. It is a Vagrant box, and since a few weeks ago there were no base boxes with the 16.04 release of Xubuntu, I had to build my own.

“4C”

In Xubuntu I use the “4C” development environment: CLion, CMake, Conan and Clang. CLion, because from what I heard it seemed to be one of the best C++ IDEs out there – and I just wanted to try it out. CMake is the build system used by CLion. I had not used either before, so the combination fits well into my learning agenda.

Conan is a package manager for C++ and basically the successor of Biicode. I had worked with several package managers for Java, Python and JavaScript, and having one for C++ seemed like something that has been missing for too long already.

I chose Clang 3.8 as the compiler, because I find the whole LLVM/Clang project very interesting. In addition, Clang had adopted the new standards a bit faster in the past, and error messages still seemed a bit better than the ones provided by GCC.

There is an article on the Conan blog about installing the “4 Cs”. I will be using at least two machines for the development, so I set up a provisioning script for the Vagrant box to automate most of the installation. You can find it on GitHub, but I’ll go through the steps here briefly.

Installation

Let’s assume you have a freshly installed Ubuntu machine. We’ll install everything from the terminal. One of the first things you’ll probably need is Git.

sudo apt-get -y install git

Install Clang

Then we want the Clang compiler, and probably LLDB as well. If we were to simply askapt-get to install clang, it would give us version 3.5 or 3.6. I want the newest version, which currently is 3.8. Since we don’t want to compile everything by explicitly calling clang++-3.8, we use update-alternatives to map clang and clang++ to clang-3.8 and clang++-3.8. For consistency we do the same for all Clang and LLDB programs.

export CLANG_VERSION=3.8
for PROG in 'clang lldb'; do
    sudo apt-get -y install ${PROG}-${CLANG_VERSION}
    for C in $(ls /usr/bin/${PROG}*${CLANG_VERSION}); do
        L=${C%-$CLANG_VERSION}
        B=$(basename $L)
        sudo update-alternatives --install $L $B $C 1000
    done
done

In the loop, $C runs over all clang* programs with version 3.8, which are on my installation:

vagrant@Xubuntu-Vagrant ~ $ ls /usr/bin/clang*3.8
/usr/bin/clang-3.8                     /usr/bin/clang-cl-3.8
/usr/bin/clang++-3.8                   /usr/bin/clang-query-3.8
/usr/bin/clang-apply-replacements-3.8  /usr/bin/clang-rename-3.8
/usr/bin/clang-check-3.8

Then, the call to update-alternatives creates a bunch of links, e.g. /usr/bin/clang++ links to /etc/alternatives/clang++, which in turn links to /usr/bin/clang++-3.8. So, whenever we call clang++ or clang-check, we actually get clang++-3.8 or clang-check-3.8.

Install CMake

This one is easy: sudo apt-get -y install cmake, and we’re done. Almost. CMake by default calls cc and c++ as compilers, unless environment variables are set that tell something different. Since I want to use only the Clang version we just installed, I’ll link cc and c++ to clang and clang++.

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang 100
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++ 100

Do not link to clang-3.8 directly, because that way if we later update Clang to another version, we only change the link of clang to that version and everything else falls into place.

Install CLion

Simply download and unpack:

sudo wget -q http://download.jetbrains.com/cpp/CLion-2016.2.tar.gz
sudo tar xfz CLion-2016.2.tar.gz -C /opt

Then run /opt/clion-2016.2/bin/clion.sh to start CLion and configure it the way you like. CLion comes with CMake installed, so you can choose whether you want that installation or the version we just installed manually. I use the manual installation since that is also the one I run when I build from command line. The default compiler in CLion is /usr/bin/c++, and we just set that to Clang, so nothing to change here.

Install Conan

Conan uses Python and can be installed as a Pip package. (Note: you don’t need to install it with Pip, there are also platform specific installers you can simply download.)

sudo apt-get -y install python-pip
sudo pip install --upgrade pip 
sudo pip install conan 
conan

That last line calls Conan and causes it to create a configuration file. There it sets the default compiler and other settings it will use to build binaries. Since you probably have a GCC installed on your system, it will use that as a default. We want it to use Clang, so call

sed -i "s/compiler=gcc/compiler=clang/; s/version=[0-9.]*/version=3.8/; s/libstdc++/libstdc++11/" ~vagrant/.conan/conan.conf

Or simply open ~/.conan/conan.conf and edit the settings. Change the compiler and version to clang and 3.8, and the standard library to libstdc++11.

Conclusion

If you have followed so far, or if you simply have used the provisioning script of the Vagrant box, you’ll have the same development environment installed I use for Fix. Of course you can use the environment for all kinds of projects. For a first example, I have a section in the README of the Vagrant box project on GitHub.

Previous Post
Next Post

13 Comments



  1. Why replace GCC globally !?
    sudo update-alternatives –install /usr/bin/cc cc /usr/bin/clang 100

    Isn’t better to just tell cmake to use clang?

    I can imagine plenty of project for linux to depend on gcc and maybe broken using clang?

    just curious.

    Also, tried using CLion but it asked for 32bit Java .. and I was like WTF 😀

    Is it really better than qtcreator and other alt ?

    Reply

    1. Hi Petar, thanks for your questions! I replace GCC globally, because I can. It’s a dedicated VM for this development environment, and I only have Fix and one or two other toy projects on it, so I don’t have trouble breaking anything. In addition, this way I can also be sure that Conan and other builds that don’t rely on CMake build everything using Clang, too. In the past I had an issue with Conan building Boost partially with GCC, because it did not use the compiler defined in Conan.

      CLion is based on IntelliJ which is built using Java. So yeah, it needs Java, but the CLion package for Linux comes with a JDK as far as I know. I can’t say if it is better than QTCreator, because I haven’t used that one yet. I have some prejudices against QT (parts of it feel clumsy and old school), which might have influenced me against using a IDE that is named after it 😉

      Reply




    1. I actually use Catch in the project, but not cppcheck. But thanks for the hint, I should add a static analyzer 🙂

      Reply

      1. clang provides a nice static analyzer.
        take a look into scan-build.

        it took me a while to get it working on my setup,
        but now i can not go back.

        Reply

      2. “… I should add a static analyzer…”
        is CLion abilities not enough in terms of static analysis ?

        by the way – do you use clang-tidy? if yes- your opinion?
        it supports (in addition to clang checks) – “C++ COre Guidelines” checks

        Reply

        1. Hi Ernst, thanks for your questions. No, there is no IDE that has all the abilities of a static analyzer. In addition, CLion currently gives me lots of false errors when I use C++14 features, Catch and Trompeloeil together. I haven’t found the time to test clang-tidy yet.

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *