【问题标题】:The std::filesystem current_path returns the root onestd::filesystem current_path 返回根目录
【发布时间】:2020-10-07 12:16:13
【问题描述】:

我只想通过std::filesystem::path::current_path(编译器是emccthis one)打印当前路径及其内容。下面是一些代码:

//... some stuff ...
namespace fs = 
#if defined(_MSC_VER) || defined(_WIN32) 
  std::filesystem;
#else
  std::__fs::filesystem;
#endif
//... some stuff ...
cfg::Config::Config() { this->Path = fs::current_path(); }

void cfg::Config::getPath()
{
    for (auto &p : fs::directory_iterator(this->Path)) std::cout << p.path() << '\n';
}

对于 Windows,它工作正常,输出如下所示:

...
"D:\\GitHub\\Emscripten_OpenGL\\build\\cmake_install.cmake"
"D:\\GitHub\\Emscripten_OpenGL\\build\\Debug"
"D:\\GitHub\\Emscripten_OpenGL\\build\\Emscripten_Graphics.dir"
"D:\\GitHub\\Emscripten_OpenGL\\build\\Emscripten_Graphics.sln"
"D:\\GitHub\\Emscripten_OpenGL\\build\\Emscripten_Graphics.vcxproj"
"D:\\GitHub\\Emscripten_OpenGL\\build\\Emscripten_Graphics.vcxproj.filters"
"D:\\GitHub\\Emscripten_OpenGL\\build\\Emscripten_Graphics.vcxproj.user"
...

但是,无论是 Linux 子系统还是带有 Ubuntu 映像的 docker 容器,它都只打印根目录:

application_1  | [100%] Built target Emscripten_Graphics
application_1  | make: make
application_1  | "/tmp"
application_1  | "/home"
application_1  | "/dev"
application_1  | "/proc"

可能是我错过了 Linux 的 std::filesystem 吗?

【问题讨论】:

  • std::__fs::filesystem; 你不应该使用任何以双下划线开头的符号。你能解释一下你这样做的方式吗?您是否忘记在 Linux 上启用 C++17?
  • 您使用的是哪个版本的GCC?当前版本有std::filesystem,旧版本有std::experimental::filesystem。不过我从没见过std::__fs::filesystem
  • @Marek R 是的,我这样做是因为在其他方面它会引发异常(std::filesystem 未找到,系统建议使用此异常)。我正在使用emcccompiler
  • 你了解工作目录的概念吗?您希望 Docker 程序在哪个工作目录中运行?您是否在 Dockerfile 中指定了任何 WORKDIR
  • 如果您使用emcc 版本编译运行 - 它会“挂载”虚拟文件系统,所有内容都从根目录开始,可能就是这种情况。

标签: c++ linux docker emscripten emcc


【解决方案1】:

我找到了一个解决方案,所以它可能对其他人有用。感谢@Sugar 我挖了Emscripten Filesystem API。 emscripten 创建自己的虚拟文件系统并上传 C++ 代码中使用的文件。根据this issue,修复它的方法是在编译时添加-s NODERAWFS=1作为标志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多