gpt4 book ai didi

c++ - “std::filesystem”在包含 后未声明

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:41 34 4
gpt4 key购买 nike

我查了很多关于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 header 。一种可能的解决方法:

#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::无处不在。

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

关于c++ - “std::filesystem”在包含 <experimental/filesystem> 后未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55474690/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com