6

I was trying to out the new filesystem STL library, but for some reason am getting errors. The Clang++7 website indicates that it should support the new filesystem library – indeed clang is running ahead of g++ I believe.

I used some code from another Stack Exchange post, so it should be valid based upon the number of upvotes. This could should go to the specified directory and print all files in that directory. Here is the code.

#include <iostream>
#include <string>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

int main(int argc, char *argv[])
{

    std::string path = "/home/.../Downloads";
    for (const auto & entry : fs::directory_iterator(path))
    {
        std::cout << entry.path() << std::endl;
    }

}

The error messages I am getting are:

CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_dir.h:188: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::directory_iterator(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::directory_options, std::error_code*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I made sure to include the experimental/filesystem header instead of just filesystem which removed any red squiggles in Clion. I tried to compile from CLion as well as from the command line. The compilation string I used was:

  clang++-7 -Wall -std=c++17 main.cpp -o app

Does anyone have a sense of what is wrong here? In the compile error messages I see the reference to std::experimental::filesystem::v1::__cxx11::.. and I am wondering why this does not say cxx17, but I was not sure if that was the cause of the issue. I explicitly indicated c++17 in the compilation string above.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
krishnab
  • 9,270
  • 12
  • 66
  • 123
  • for clang >= 10 See https://stackoverflow.com/questions/33149878/experimentalfilesystem-linker-error/64982538#64982538 – puio Nov 24 '20 at 08:18

1 Answers1

14

filesystem is still experimental and requires an extra library.

If you are using libstdc++, link with -lstdc++fs (or target_link_libraries(${PROJECT_NAME} stdc++fs)).

For libc++, use -lc++fs (similar for the CMake command).

krishnab
  • 9,270
  • 12
  • 66
  • 123
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • 1
    haha, you got it. Yeah, I did not see anyone mention this `-lstdc++fs` flag. Thanks so much for your help on that. – krishnab Dec 16 '18 at 22:30
  • Yes, it's rather unfortunate. If I understand things properly, it's the same for all compilers due to potential ABI changes in the near future. I may be wrong. – Matthieu Brucher Dec 16 '18 at 22:31