繁体   English   中英

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

[英]'std::filesystem' has not been declared after including <experimental/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

看来您的C ++ 17编译器不包含标准filesystem标头。 解决该问题的一种可能方法:

#ifndef __has_include
  static_assert(false, "__has_include not supported");
#else
#  if __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::

您使用std::filesystem::directory_iterator std::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。

Cpp 17 不理解标准库。
在 Cmake.txt 添加:

set (CMAKE_CXX_FLAGS "-lstdc++fs -std=c++17")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM