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

时间:2019-01-23 21:07:58

标签: c++ c++-experimental

我正在尝试使用experimental :: filesystem下的文件权限,但是它声明未声明perm_options。我尝试设置标志lstdc++fs以及std=c++14std=c++17,但无济于事。我从参考站点复制了测试代码,并且该代码也无法编译。测试代码如下:

#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");
}

我正在使用g ++ 7.3.0的Ubuntu 18.04.1 LTS。

编译时出现错误:

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

我不确定std :: experimental :: filesystem是否仅缺少对此功能的支持,但是缺少这一部分似乎很奇怪。在这方面的任何帮助或指示,将不胜感激。

编辑:

好的,所以我不知道g ++ 8,因为Ubuntu告诉我我的g ++是最新的。我使用命令sudo apt-get install g++-8安装了g ++ 8.2.0,我似乎能够将其与命令g++-8而非g++一起使用。我使用标准cout测试了该编译器,以确保其可以编译。我将include和namespace替换为:

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

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

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 个答案:

答案 0 :(得分:0)

您尝试调用的filesystem::permissions的重载在实验分支中不存在。每个cppreference唯一的重载是

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

并且filesystem::perm_options甚至不是type就是technical specification

C ++ 17的std::filesystem确实有std::filesystem::perm_options,并且filesystem::permissions重载了std::filesystem::perm_options

您可以看到它在live example on wandbox中与GCC 8.1一起工作(在7.3中不受支持)。请注意,由于默认情况下它不链接,因此必须在编译器选项中使用-lstdc++fs才能使它工作。