std::filesystem需要c++17才支持;即要求vs2017或者gcc8.0以上才可以;
一、CMakeLists.txt中打开c++17支持
#set compile options
if("${CMAKE_SYSTEM_NAME}" STREQUAL "")
set(CMAKE_SYSTEM_NAME "Windows") #should in sh or cmake commands add this
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set( os_std_pre "/std:" )
else()
set( os_std_pre "-std=" )
endif()
set( str_cxx11 "${os_std_pre}c++11" )
set( str_cxx17 "${os_std_pre}c++17" ) # above gcc8.0 or msvc2017
include(CheckCXXCompilerFlag )
CHECK_CXX_COMPILER_FLAG( "${str_cxx17}" _cpp_17_flag_supported ) #无语,这个检测函数还不好使
if ( TRUE )
#if(_cpp_17_flag_supported )
set( CMAKE_CXX_STANDARD 17 )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${str_cxx17}")
message(STATUS "Using flag -std=c++17.")
endif()
二、遍历文件夹
bool FindFilesInFolder(std::string strPath, std::vector<std::string> &vecFiles)
{
char cEnd = *strPath.rbegin();
if (cEnd == '\\' || cEnd == '/')
{
strPath = strPath.substr(0, strPath.length() - 1);
}
if (strPath.empty() || strPath == (".") || strPath == (".."))
return false;
std::error_code ec;
std::filesystem::path fsPath(strPath);
if (!std::filesystem::exists(strPath, ec)) {
return false;
}
// const std::string str1 = str.path().filename().string();
// std::cout << std::filesystem::absolute(str.path()) << '\n'; //绝对路径
// std::cout << std::filesystem::absolute(str.path()).string() << '\n';
for (auto &itr : std::filesystem::directory_iterator(fsPath))
{
if (std::filesystem::is_directory(itr.status()))
{
FindFilesInFolder(itr.path().string(), vecFiles);
}
else
{
//if (std::regex_match(filename.string(), fileSuffix))
//std::filesystem::remove_all(version_dir, ec);
//std::filesystem::copy_file(from_file, to_file, std::filesystem::copy_options::skip_existing, ec))
vecFiles.push_back(itr.path().string());
}
}
return true;
}