std::experimental::filesystem::p erm_options 尚未声明
std::experimental::filesystem::perm_options has not been declared
我正在尝试在实验性::文件系统下使用文件权限,但它指出perm_options没有声明。我尝试lstdc++fs
以及std=c++14
和std=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_options
,filesystem::permissions
超载了,无法std::filesystem::perm_options
.
您可以在带有GCC 8.1的wandbox上的这个实时示例中看到它的工作(7.3不支持)。 请注意,您必须在编译器选项中使用-lstdc++fs
才能使其工作,因为它默认情况下不链接。
- #ifdef和未声明的标识符
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 在气泡排序程序中未声明错误功能
- 我在范围内未声明的错误类有问题
- 使用未声明的标识符"sqrt"?
- 我包含了<winuser.h>,但编译器在MonitorFromWindow上抛出"未声明"
- 对类 (C++) 中的私有变量使用未声明的标识符
- 我的代码中有错误,未声明的标识符
- 未定义的标识符/未声明
- 错误:在 C++ 'std'使用未声明的标识符
- 在编译器之前检测未声明的标识符
- 使用未声明的标识符'temp'
- 使用未声明的标识符"yylex"和"yyin"?
- 使用"class"关键字,后跟未声明的标识符
- 未声明的标识符错误,但变量定义正确 (?)
- C2065 'cout':未声明的标识符
- 在此范围内未声明错误"Clreol"
- C++模板模板参数中未声明的标识符