Eventually it comes a time when you have to use a C++11 piece of software which, by default, doesn’t compile on Linux. This happens because Linux distributions use the GNU libstdc++ library as the default implementation of the C++ standard. Unfortunately the libstdc++ is not (yet) C++11 features-complete, because there are some C++11 features which are currently not implemented. Consider for instance the following C++11 program:

#include <fstream>

int main()
{
    auto stream = std::ifstream("~/test.txt");

    return 0;
}

Even such a trivial program cannot be compiled using gcc (gcc 4.9.1 at the time of writing). If you try, you’ll get this kind of error:

$ g++ -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:5:45: error: use of deleted function ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’
     auto stream = std::ifstream("~/test.txt");
                                             ^

A good solution to this problem is to switch to a C++ implementation which is C++11 complete: we are talking about the LLVM libc++ library. This library has been created mainly due to license problems, since Apple and the BSDs don’t like the GPLv3 used by libstdc++. So, we can say that Linux is not a first class target of libc++ developers, and there are also some religious debates about whether Linux users should use libc++ in their systems. But actually I’ve just tested libc++ on my Debian Sid and it works remarkably well. You just need to install these packages:

  • libc++1
  • libc++-dev
  • libc++abi
  • libc++abi-dev

You need also to install and use clang instead of gcc, since at the moment I don’t think it’s possible to use gcc togheter with libc++. You can finally compile every C++11 program using: clang++ -std=c++11 -stdlib=libc++

Of course on other distributions different from Debian, the name of those packages may slightly change. The great thing is that libc++ and libstdc++ are coinstallable and they can perfectly coexist. If you want you can use libc++ just for C++11 programs and libstdc++ for all the other stuff.