包含 <experimental/filesystem> 后未声明“std::filesystem”

新手上路,请多包涵

查了很多关于c++17下文件系统链接的问题,还是不能成功链接。我的 main.cpp 文件如下。

 #include <experimental/filesystem>

int main(int argc, char** argv)
{
    std::string imageDirectory = "./image";;
    std::vector<std::string> imagePath;

    for (const auto& entry: std::filesystem::directory_iterator(imageDirectory))
    {
        imagePath.push_back(entry.path());
        std::cout << entry.path() << std::endl;
    }

    return 0;
}

我的 CMakeLists.txt 如下。

 cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

project(visual_hull LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
add_library(dataIO
        STATIC
            dataIO.hpp
            dataIO.cpp)

find_package(OpenCV REQUIRED core highgui imgproc)

target_link_libraries(dataIO ${OpenCV_LIBS})

add_executable(visual_hull main.cpp)

target_link_libraries(visual_hull PUBLIC dataIO
                                         stdc++fs)

错误如下。

 /home/SENSETIME/zhangshunkang/Downloads/programming/c++/visual_hull/main.cpp: In function ‘int main(int, char**)’:
/home/SENSETIME/zhangshunkang/Downloads/programming/c++/visual_hull/main.cpp:15:31: error: ‘std::filesystem’ has not been declared
  for (const auto& entry: std::filesystem::directory_iterator(imageDirectory))
                               ^
CMakeFiles/visual_hull.dir/build.make:62: recipe for target 'CMakeFiles/visual_hull.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/visual_hull.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/visual_hull.dir/all' failed
make[1]: *** [CMakeFiles/visual_hull.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

原文由 Sean 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.2k
2 个回答

您使用 std::filesystem::directory_iteratorstd::filesystem::directory_iterator 和整个命名空间 std::filesystem 在头文件 <filesystem> --- 中声明。

您还没有包含标题 <filesystem> 。相反,您已经包含 <experimental/filesystem> 。此标头未声明 std::filesystem::directory_iterator 。相反,它声明 std::experimental::filesystem::directory_iterator

您可以始终使用标准文件系统库或技术规范中的实验文件系统库,但不能将它们混合在一起。如果您的目标是 C++17,那么您应该使用 <filesystem>

我会得到文件系统的错误:没有这样的文件或目录

理想的解决方案是将编译器升级到对 C++17 具有非实验性支持的版本。否则使用实验性 TS 或 Boost。

原文由 eerorika 发布,翻译遵循 CC BY-SA 4.0 许可协议

您的 C++17 编译器似乎不包含标准的 filesystem 标头。一种可能的解决方法:

 #ifndef __has_include
  static_assert(false, "__has_include not supported");
#else
#  if __cplusplus >= 201703L && __has_include(<filesystem>)
#    include <filesystem>
     namespace fs = std::filesystem;
#  elif __has_include(<experimental/filesystem>)
#    include <experimental/filesystem>
     namespace fs = std::experimental::filesystem;
#  elif __has_include(<boost/filesystem.hpp>)
#    include <boost/filesystem.hpp>
     namespace fs = boost::filesystem;
#  endif
#endif

然后在任何地方使用 fs:: 而不是 std::filesystem::

如果您想在使用 C++1114 时使用 filesystem ,检查 __cplusplus >= 201703L 只是一个额外的预防措施。在这些情况下, __has_include(<filesystem>) 可能是 true 但包含它不会定义 std::filesystem 命名空间。

原文由 Ted Lyngmo 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题