clang++-12 couldn't find library ranges
Asked Answered
T

1

10

I am testing ranges from C++ 20 , and this is my main.cpp:

#include <ranges>
#include <iostream>

int main()
{
    auto const ints = {0,1,2,3,4,5};
    auto even = [](int i) { return 0 == i % 2; };
    auto square = [](int i) { return i * i; };

    for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
        std::cout << i << ' ';
    }

    std::cout << '\n';
}

To compile it with clang++-12 but it couldn't find 'ranges':

$ clang++-12 --version
Ubuntu clang version 12.0.1-++20210525082622+328a6ec95532-1~exp1~20210525063352.95
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin


$ clang++-12 main.cpp
cpp_ranges.cpp:1:10: fatal error: 'ranges' file not found
#include <ranges>
         ^~~~~~~~
1 error generated.

to call clang++12 with --std=c++20 doesn't make a difference.

How could I fix it?

Tenebrous answered 26/5, 2021 at 8:56 Comment(5)
It seems that clang only has partial support for ranges, and only in version 13: en.cppreference.com/w/cpp/compiler_supportPohai
Are you using libc++? The version of clang isn't relevant to ranges support, if you're on Linux I think clang defaults to using your system version of libstdc++Lessard
@AlanBirtles I'm using Linux, but have never heard about libstdc++...Tenebrous
gcc.gnu.org/onlinedocs/libstdc++Lessard
@rahn C++ defines the C++ standard library, which has multiple implementations. The most used ones are libstdc++ (provided by GNU), libc++ (provided by LLVM), and Microsoft STL. On a Linux system, typically, libstdc++ is used by default even with a Clang compiler. Which might be your case. I don't know of any sophisticated check, but you can simply print sizeof(std::string). With libstdc++, it gives 32, while with libc++, it is 24. Or, you can ldd your program file.Cicala
R
0

Ranges was introduced to C++20 in P0896. Cppref reports that this library feature is supported by libstdc++ 10 and newer and partially supported by libc++ 13. By default clang on linux will use libstdc++ so check what version of libstdc++ you have or update clang and use -stdlib=libc++.

Rockoon answered 6/9 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.