std::experimental::filesystem::p erm_options 尚未声明

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

本文关键字:options 未声明 experimental filesystem std erm      更新时间:2023-10-16

我正在尝试在实验性::文件系统下使用文件权限,但它指出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++ 18.04.1 LTS 的 7.3.0 上。

编译时出现错误:

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 <filesystem>
namespace fs = std::filesystem;

起初它说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

您尝试调用的filesystem::permissions的重载在实验分支中不存在。 根据 cpp首选项,唯一的重载是

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

filesystem::perm_options甚至不是技术规范type

C++17的std::filesystem确实有std::filesystem::perm_optionsfilesystem::permissions超载了,无法std::filesystem::perm_options.

您可以在带有GCC 8.1的wandbox上的这个实时示例中看到它的工作(7.3不支持)。 请注意,您必须在编译器选项中使用-lstdc++fs才能使其工作,因为它默认情况下不链接。