最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - MSYS2 Clang++ produces error with filesystem library - Stack Overflow

programmeradmin0浏览0评论

I am using clang++, installed with MSYS2 on Windows 11. Checking the version of clang++ gives the below.

~ clang++ --version
clang version 19.1.4
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/mingw64/bin

The code:

#include <filesystem>

int main(){
    std::filesystem::path p = "C:/Users/Jaden/Documents/hi.txt";
    std::filesystem::path p2 = "C:/Users/Jaden";

    std::filesystem::copy(
        p, p2, std::filesystem::copy_options::overwrite_existing);
}

Produces the error:

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot copy: File exists [C:/Users/Jaden/Documents/hi.txt] [C:/Users/Jaden]

If I run std::filesystem::exists beforehand, it returns false. The error persists no matter if I use or do not use and copy_option flags.

The code was simply compiled with clang++ .\test.cpp.

Edit: Similarly when I compile using g++, the same issue persists.

I am using clang++, installed with MSYS2 on Windows 11. Checking the version of clang++ gives the below.

~ clang++ --version
clang version 19.1.4
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/mingw64/bin

The code:

#include <filesystem>

int main(){
    std::filesystem::path p = "C:/Users/Jaden/Documents/hi.txt";
    std::filesystem::path p2 = "C:/Users/Jaden";

    std::filesystem::copy(
        p, p2, std::filesystem::copy_options::overwrite_existing);
}

Produces the error:

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot copy: File exists [C:/Users/Jaden/Documents/hi.txt] [C:/Users/Jaden]

If I run std::filesystem::exists beforehand, it returns false. The error persists no matter if I use or do not use and copy_option flags.

The code was simply compiled with clang++ .\test.cpp.

Edit: Similarly when I compile using g++, the same issue persists.

Share Improve this question edited Jan 29 at 20:17 JadenJin asked Jan 29 at 20:07 JadenJinJadenJin 931 silver badge4 bronze badges 5
  • Can you successfully copy hi.txt to a subdirectory of Documents? Anything change if you use MSYS2-style paths (/c/Users/Jaden/Documents/hi.txt)? – genpfault Commented Jan 29 at 20:20
  • @genpfault Strange thing happened. If I create a new sub-directory and I run the script, nothing will happen. If I run the script again the error will occur. Trying to use MingW-style paths results in a directory does not exist error. – JadenJin Commented Jan 29 at 20:26
  • Is it trying to make C:/Users/Jaden/Documents/hi.txt into C:/Users/Jaden as in into the folder which DOES exist? "C:/Users/Jaden/hi.txt" behaves as expected. – user4581301 Commented Jan 29 at 20:26
  • cppreference says Otherwise, if from is a regular file, then ... Otherwise, if to is a directory, then behaves as if copy_file(from, to/from.filename(), options) (creates a copy of from as a file in the directory to) which reads to me as this should work, but in the modern Windows world of groovy virtual folders representing other folders (sometimes in cloud storage) is the user folder REALLY a folder? – user4581301 Commented Jan 29 at 20:43
  • Also fails with a plain a folder as I can make: File exists [D:/test/a/hi.txt] [D:/test/b] – user4581301 Commented Jan 29 at 20:46
Add a comment  | 

1 Answer 1

Reset to default 5

After some testing, this is a bug in libstdc++, the GCC's C++ standard library. It's already fixed in the newer versions.

This happens any time you do std::filesystem::copy("file", "directory"), regardless of overwrite_existing.

I've reported it here: https://gcc.gnu./bugzilla/show_bug.cgi?id=118699, and was told that it's fixed in 15.x, 14.3.0, and 13.4.0, while MSYS2 still sits at 14.2.

发布评论

评论列表(0)

  1. 暂无评论