简体   繁体   English

std :: experimental :: filesystem :: perm_options尚未声明

[英]std::experimental::filesystem::perm_options has not been declared

I am attempting to work with file permissions under experimental::filesystem but it states perm_options is not declared. 我正在尝试使用experimental :: filesystem下的文件权限,但是它声明未声明perm_options。 I have tried setting flags lstdc++fs as well as std=c++14 and std=c++17 but to no avail. 我试过设置标志lstdc++fs以及std=c++14std=c++17但无济于事。 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. 我在使用g ++ 7.3.0的Ubuntu 18.04.1 LTS上。

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. 我不确定std :: experimental :: filesystem是否仅缺少对此功能的支持,但是缺少这一部分似乎很奇怪。 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. 好的,所以我不知道g ++ 8,因为Ubuntu告诉我我的g ++是最新的。 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++ . 我使用命令sudo apt-get install g++-8安装了g ++ 8.2.0,我似乎能够将其与命令g++-8而不是g++ I tested this compiler with a standard cout to make sure it compiles. 我使用标准cout测试了此编译器,以确保其可以编译。 I replaced the include and namespace to this: 我将include和namespace替换为:

#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: 最初它说filesystem没有定义,但是我可以用-std=c++17标志来解决这个问题,还添加了-lstdc++fs标志,但是现在出现了错误:

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

The overload of filesystem::permissions that you are trying to call does not exist in the experimental branch. 实验分支中不存在您尝试调用的filesystem::permissions重载。 Per cppreference the only overloads are 每个cppreference唯一的重载是

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 . filesystem::perm_options甚至不是type技术规范

C++17's std::filesystem does have std::filesystem::perm_options and filesystem::permissions is overloaded to take a std::filesystem::perm_options . C ++ 17的std::filesystem确实具有std::filesystem::perm_options并且filesystem::permissions重载以采用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). 您可以在带有GCC 8.1的wandbox上的此实时示例中看到它的运行情况(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. 请注意,由于默认情况下它未链接,因此必须在编译器选项中使用-lstdc++fs才能使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM