2

I want to use the C++17 std::optional but it seems to be absent in clang:

> cat test.cxx 
#include <optional>

int main(int, char **) {
    return 0;
}
> $CXX --version | head -n1
clang version 6.0.0 (trunk 317775)
> $CXX -std=c++17 test.cxx 
test.cxx:1:10: fatal error: 'optional' file not found
#include <optional>
         ^~~~~~~~~~
1 error generated.

As you can see I am using a rather new version of clang and, as far as I know, clang 6 should have complete C++17 support. On first glance it looks like this is an clang issue, especially because including <experimental/optional> works fine, but maybe it is me who is missing something. Do you have any ideas?

Thanks

avitase
  • 134
  • 1
  • 8
  • 3
    Are you on linux with the latest GCC too? I think clang on linux uses GCC standard library. So to use Clang with C++17 on linux, make sure you have the latest libstdcpp. Consider using clang's libc++ if you can. – Guillaume Racicot Nov 20 '17 at 16:47
  • 1
    clang is just the compiler. What you're missing is an updated version of the standard library. – Rakete1111 Nov 20 '17 at 16:50
  • Thanking you both for these clarifications. So the solution is to (re)compile clang with its own libc++? – avitase Nov 20 '17 at 16:50
  • No, it's to use a newer standard library to compile your code. It doesn't matter what standard library clang used to compile itself. – Rakete1111 Nov 20 '17 at 16:55
  • 1
    Isn't building LLVM/Clang with libcxx, checked out at llvm/projects, one way to do this upgrade? – avitase Nov 20 '17 at 17:02
  • @avitase No, you don't understand. **Your** system library needs updating, not the one used to compile clang. Sure, you can download the trunk version and install that if you want to. – Rakete1111 Nov 20 '17 at 17:03
  • Apologies for being imprecise. I don't want a system-wide installation. I meant to use the trunk version that comes with LLVM as a replacement for my system library. Thanks to your help this works fine now. – avitase Nov 21 '17 at 17:44
  • 2
    @Rakete1111, building LLVM with libcxx in the tree doesn't mean using that std::lib to compile clang, it means that libc++ will be installed alongside Clang and so can be used with it (just as avitase said). – Jonathan Wakely Dec 13 '17 at 13:38

1 Answers1

5

As pointed out in the comments, Clang is probably using the system's libstdc++ headers by default, and your system's libstdc++ is too old to have C++17 support.

Either install a newer GCC (which comes with a newer libstdc++) and then tell Clang how to find it using the --gcc-toolchain=/path/to/new/gcc option, or install libc++ alongside Clang and tell it to use libc++ with the -stdlib=libc++ option.

If you choose to install a newer libstdc++ then you will need at least GCC 7.1, which was the first to provide <optional>.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 1
    Or compile clang against the new gcc toolchain (took me a whole weekend [to figure out how](https://stackoverflow.com/a/47743951/2069064)...) – Barry Dec 13 '17 at 15:12