std::experimental::filesystem::perm_options has not been declared

2.6k Views Asked by At

I am attempting to work with file permissions under experimental::filesystem but it states perm_options is not declared. I have tried setting flags lstdc++fs as well as std=c++14 and std=c++17 but to no avail. I copied the test code from a reference site and it does not compile either. Test code follows:

#include <fstream>
#include <bitset>
#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;

void demo_perms(fs::perms p)
{
    std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
              << ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
              << ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
              << ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
              << ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
              << ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
              << ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
              << ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
              << ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
              << '\n';
}
int main()
{
    std::ofstream("test.txt"); // create file

    std::cout << "Created file with permissions: ";
    demo_perms(fs::status("test.txt").permissions());

    fs::permissions("test.txt",
                    fs::perms::owner_all | fs::perms::group_all,
                    fs::perm_options::add);

    std::cout << "After adding o+rwx and g+rwx:  ";
    demo_perms(fs::status("test.txt").permissions());

    fs::remove("test.txt");
}

I am on Ubuntu 18.04.1 LTS using g++ 7.3.0.

When I compile I get the error:

test.cpp: In function ‘int main()’:
test.cpp:72:26: error: ‘fs::perm_options’ has not been declared
                  fs::perm_options::add);

I'm not sure if std::experimental::filesystem is just lacking support for this feature, but it seems odd that this one part is missing. Any help or direction in the matter would be greatly appreciated.

EDIT:

Okay, so I was not aware of g++8 as Ubuntu had told me that my g++ was up to date. I installed g++8.2.0 using the command sudo apt-get install g++-8 and I seem to be able to use it with the command g++-8 instead of g++. I tested this compiler with a standard cout to make sure it compiles. I replaced the include and namespace to this:

#include <filesystem>
namespace fs = std::filesystem;

At first it said filesystem was not defined but I was able to take care of that with the flag -std=c++17 and also added the flag -lstdc++fs but now am getting the errors:

g++-8 -std=c++17 -lstdc++fs test.cpp -o test
/tmp/ccs3Eg7a.o: In function `main':
test.cpp:(.text+0x259): undefined reference to 
`std::filesystem::status(std::filesystem::__cxx11::path const&)'
test.cpp:(.text+0x2c7): undefined reference to 
`std::filesystem::permissions(std::filesystem::__cxx11::path const&, 
std::filesystem::perms, std::filesystem::perm_options)'
test.cpp:(.text+0x313): undefined reference to 
`std::filesystem::status(std::filesystem::__cxx11::path const&)'
test.cpp:(.text+0x369): undefined reference to 
`std::filesystem::remove(std::filesystem::__cxx11::path const&)'
/tmp/ccs3Eg7a.o: In function `std::filesystem::__cxx11::path::path<char [9], std::filesystem::__cxx11::path>(char const (&) [9], std::filesystem::__cxx11::path::format)':
test.cpp:(.text._ZNSt10filesystem7__cxx114pathC2IA9_cS1_EERKT_NS1_6formatE[_ZNSt10filesystem7__cxx114pathC5IA9_cS1_EERKT_NS1_6formatE]+0x6d): undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
1

There are 1 best solutions below

3
On

The overload of filesystem::permissions that you are trying to call does not exist in the experimental branch. Per cppreference the only overloads are

void permissions(const path& p, perms prms);
void permissions(const path& p, perms prms, error_code& ec);

and filesystem::perm_options is not even a type is the technical specification.

C++17's std::filesystem does have std::filesystem::perm_options and filesystem::permissions is overloaded to take a std::filesystem::perm_options.

You can see it working in this live example on wandbox with GCC 8.1 (It is not supported in 7.3). Do note that you have to use -lstdc++fs in the compiler options to get it to work as it does not link by default.