我正在尝试在experimental::filesystem 下使用文件权限,但它指出未声明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++ 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++ 是最新的。我使用命令安装了 g++8.2.0 sudo apt-get install g++-8
,我似乎可以使用命令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