C++17 std::variant header file (clang 6.0.0)
Asked Answered
A

1

8

Update

I narrowed down the problem to (probably! it's not entirely clear, even reading all I could find about the topic) that installing stdlibc++-7-dev would provide me with suitable (i.e., C++17-compliant) STL headers and libraries.

This (also, apparently) comes bundled with Ubuntu 17.04 (artful?) but is not available for xenial (Ubuntu 16.04.3 LTS) which is what I'm using.

I have tried downloading the individual .deb packages and installing them, but it quickly becomes a maze of unresolved dependencies.

If anyone could point me to how to install libstdc++-7-dev on 16.04, I'd be most grateful.

Original question

I have just installed clang++ 6.0 in Ubuntu 16.04 via the package manager (following these instructions) and all seems well: /usr/bin/clang++-6.0 works just fine, and if I try to use something that only works in C++17 (non-type template arguments with auto, see here) it compiles and runs, once I set CMAKE_CXX_COMPILER=/usr/bin/clang++-6.0 -- while it fails when I don't.

So... clang 6.0 understands C++17 as advertised (doh!) but when I use:

#include <variant>

the file is not found where I would expect it to be:

$ ll /usr/include/clang/6.0.0/
total 0
lrwxrwxrwx 1 root root 45 Aug  6 21:32 include -> ../../../lib/llvm-6.0/lib/clang/6.0.0/include

or anywhere else I can think of.

Would anyone know (a) whether it's supposed to be there at all and (b) if so, where do I go find it?

Update

I have double-checked that I have the latest (I think) stdc++ library:

$ sudo apt-get install libstdc++-5-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libstdc++-5-dev is already the newest version (5.4.0-6ubuntu1~16.04.4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

and same for libstdc++-6-dev; also, I have libc++-dev:

$ sudo apt-get install libc++-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libc++-dev is already the newest version (3.7.0-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Still, the variant.h* file is nowhere to be found. Anything else I should try?

Accusative answered 13/8, 2017 at 6:30 Comment(14)
Try adding -stdlib=libc++ to both the compiler and the linker.Binocular
Just a quick question: Does Clang 4.0.0 support C++17 or is it only 6.0? This is the first time I'm hearing about 6.0Toshiatoshiko
Some parts are supported, but still under the c++1z flag (I don't expect this to change until the standard is ratified). I think they've switched to 6 in development as they're getting ready to release 5.Binocular
Clang is a compiler. The standard library is separate.Replica
@Replica yes, I get that, my question was, how do I get a C++17-compatible STL? I thought it would come installed with the other packages, but clearly it wasn't.Accusative
@kim366 just found this page: en.cppreference.com/w/cpp/compiler_support looks like clang-4 supports most features, but a couple only came out with 5.Accusative
@Marco Okay. I just find it weird that there is no mention about Clang 6.0 (releases.llvm.org/download.html#4.0.1)Toshiatoshiko
Anyways, maybe try including <experimental/variant>. That's what I was doing with <optional>, before I updated my compilerToshiatoshiko
Thanks, @kim366, that helped narrow it a bit: I found /usr/include/c++/5/experimental/ which seems to be the bits supporting C++14 - still no variant header in that folder, but now I'm guessing it's a case of tracking down which package adds the header files for the STL to support C++17Accusative
@kim366 There is no such thing as <experimental/variant>.Replica
@Replica It was worth a try..Toshiatoshiko
You can update to gcc 7.1 and use libstdc++ through clang.Toshiatoshiko
Clang 5.0 is about to be released. (probably early September). 6.0 is the next release - probably March 2018.Overcheck
@kim366 clang 6 is a dev branch apt.llvm.org. I'm also confused about how to get c++17 headers. string_view is part of the standard now and experimental/string_view is from TS.Epirus
O
3

Yes, clang 5.0 (or rather, the libc++ that will ship as part of clang 5) has the <variant> header. But you need to be sure that you have installed libc++.

And as @KayEss mentioned, you'll need to pass -std=c++17 (or the earlier version of the same flag -std=c++1z) because variant is a C++17-only feature.

Overcheck answered 15/8, 2017 at 5:6 Comment(2)
Can you please elaborate on the "you need to be sure that you have installed libc++" part? I have installed clang++-6 package (and verified it works fine) along with other packages that were "suggested" by apt-get: however, when I looked into the libstdc++ packages to install, I didn't see anything obvious to install. (and, yes, I already use the --std=c++17 flag - but that would fail the compilation: it's the header file that's missing)Accusative
When you install clang for (say) Ubuntu, it comes set up to use the already installed C++ standard library (libstdc++). This is helpful, because it means that you can "mix and match" object files compiled with gcc and clang. You need to install the libcxx package as well (example here: launchpad.net/ubuntu/+source/libc%2B%2B) and pass -std=libc++ to clang.Overcheck

© 2022 - 2024 — McMap. All rights reserved.