Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fatal error: 'charconv' file not found in clang 6.0 with -std=c++17

Recently, I wanted to use from_chars from c++17. Looked at http://en.cppreference.com/w/cpp/utility/from_chars and found that code on this page:

#include <iostream>
#include <charconv>
#include <array>

int main()
{
    std::array<char, 10> str{"42"};
    int result;
    std::from_chars(str.data(), str.data()+str.size(), result);
    std::cout << result;
}

cannot be compiled by any of compilers. I tried on page http://en.cppreference.com/w/cpp/utility/from_chars and on godbolt with different compilers but all of them returned the same error:

<source>:2:10: fatal error: 'charconv' file not found
#include <charconv>
         ^~~~~~~~~~
1 error generated.
Compiler returned: 1

Can anybody help me with this, please?

(I tried clang 6.0, gcc 7.3 and msvc 19 but all of them returned error that 'charconv' not found)

like image 339
ffilosoff Avatar asked Apr 03 '18 22:04

ffilosoff


People also ask

How do you use C++17 in Clang?

C++17 implementation status You can use Clang in C++17 mode with the -std=c++17 option (use -std=c++1z in Clang 4 and earlier).

Does Clang work for C++?

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code. Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP.

Is Clang compatible with GCC?

GCC and C99 allow an array's size to be determined at run time. This extension is not permitted in standard C++. However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs. If you would prefer not to use this extension, you can disable it with -Werror=vla.


1 Answers

According to GCC's libstdc++ status page, this header is only supported starting from GCC-8.1. You could either avoid using this header by detecting it:

#if __has_include(<charconv>)
#include <charconv>
#else
/* implement some fallback */
#endif

Or just update your compiler. This godbolt example confirms that the header is present in GCC-8.1.

If you use Clang, remember that Clang uses GCC's stdlibc++ by default. You will either need to update your GCC or switch to Clang's libc++ by:

clang++ -std=c++17 -stdlib=libc++ main.cpp
like image 147
ivaigult Avatar answered Nov 14 '22 21:11

ivaigult