0

The latest versions of GCC and clang support C++17's std::filesystem (ref).

Why does this program not compile?

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::create_directories("sandbox/1/2/a");
    fs::create_directory("sandbox/1/2/b");
 //   fs::permissions("sandbox/1/2/b", fs::perms::remove_perms | fs::perms::others_all);
    fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
    std::system("ls -l sandbox/1/2");
    fs::remove_all("sandbox");
}

I tested it with the latest compiler versions from an online compiler, with the following compiler options:

clang++ prog.cc -Wall -Wextra -std=c++17
g++ prog.cc -Wall -Wextra -std=c++2a

From clang I get these linker errors:

/tmp/prog-9b6259.o: In function `main':
prog.cc:(.text+0x48): undefined reference to `std::__1::__fs::filesystem::__create_directories(std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
prog.cc:(.text+0xad): undefined reference to `std::__1::__fs::filesystem::__create_directory(std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
prog.cc:(.text+0x12f): undefined reference to `std::__1::__fs::filesystem::__create_directory(std::__1::__fs::filesystem::path const&, std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
prog.cc:(.text+0x1b5): undefined reference to `std::__1::__fs::filesystem::__remove_all(std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
1

If I decomment the line with fs::permissions, I get this compile time error:

prog.cc:11:49: error: no member named 'remove_perms' in 'std::__1::__fs::filesystem::perms'
    fs::permissions("sandbox/1/2/b", fs::perms::remove_perms | fs::perms::others_all);
                                     ~~~~~~~~~~~^

I get similar errors from GCC.

What am I doing wrong? Is it possible that these compilers support the C++17 language, but not its libraries in their entirety?

Pietro
  • 12,086
  • 26
  • 100
  • 193
  • 8
    tldr; You need to add `-lstdc++fs` (still true as far as I'm aware) – Barry Sep 20 '18 at 14:58
  • 2
    The compiler error is because there is no `perms::remove_perms` in C++17, check a proper reference for the standard library, e.g. https://en.cppreference.com/w/cpp/filesystem/perms and https://en.cppreference.com/w/cpp/filesystem/perm_options – Jonathan Wakely Sep 20 '18 at 15:03
  • @Barry - You are right, it works with GCC. – Pietro Sep 20 '18 at 15:09
  • @JonathanWakely - Strange, with a search on `en.cppreference.com` I can find a pointer to `std::filesystem::perms::remove_perms`, but when I click on it I go to a page where it is not mentioned. Could have it been removed just before freezing the standard? – Pietro Sep 20 '18 at 15:14
  • 1
    Yes, not long before. It was changed by http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0492r2.html#Late37 – Jonathan Wakely Sep 20 '18 at 15:17
  • 4
    For Clang's libc++ you need to [link with `-lc++fs`](https://libcxx.llvm.org/docs/UsingLibcxx.html#using-filesystem-and-libc-fs) – Jonathan Wakely Sep 20 '18 at 15:18
  • @JonathanWakely [it works](https://wandbox.org/permlink/3Il8ALACYnfOZaOJ) thanks. – Marek R Sep 20 '18 at 15:33
  • If anybody can write an answer, I will accept it. – Pietro Sep 21 '18 at 13:03

0 Answers0