0

I'm having problems getting any output from the std::filesystem functions in C++, even from the most simple functions like current_path. I'm compiling with mingw64, g++ on Windows, it comes with C++17 support and experimental C++20 support which I've enabled, but regardless of C++ version, I'm getting no output. This is the tasks.json I'm using with Visual Studio code:

"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
    "-fdiagnostics-color=always",
    "-g",
    "${file}",
    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe",
    "-std=gnu++20"
],
"options": {
    "cwd": "${fileDirname}"
},
"problemMatcher": [
    "$gcc"
],  
"group": "build",
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"

I've tried running some very simple examples from here, such as:

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    std::cout << "Current path is " << fs::current_path() << '\n'; // (1)
    fs::current_path(fs::temp_directory_path()); // (3)
    std::cout << "Current path is " << fs::current_path() << '\n';
}

Which returns no output at all. Another thing I wanted to do was listing the files in a certain directory like this:

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    std::string path = "C:\\Users\\MyName\\Desktop";
    for (const auto & entry : fs::directory_iterator(path))
        std::cout << entry.path() << std::endl;
}

Which again, returns absolutely no output. I don't know what I'm doing wrong and I've not been able to find anybody else with this problem.

While writing the question I've found an answer that works here, that is, adding the -static argument to g++ to avoid dynamic linking. I'm not familiar with these minutiae yet, but is there no way to avoid static building in my case?

  • Try running your program from the explorer with a double-click. See it you get any error messages. Also try running from the MSYS terminal (the `mingw64.exe` one). – HolyBlackCat Apr 04 '22 at 07:43
  • yeah, I cannot reproduce this (but then again, I'm using a mac, so... yeah). – WhozCraig Apr 04 '22 at 07:46
  • @HolyBlackCat, when running through explorer by double clicking everything works fine without static building! Why doesn't it output anything when running through the regular terminal? What is happening? – blundered_bishop Apr 04 '22 at 07:50
  • The difference has to be in the PATH. – HolyBlackCat Apr 04 '22 at 07:59
  • @HolyBlackCat I've followed the instructions at [C/C++ for Visual Studio Code](https://code.visualstudio.com/docs/languages/cpp) and added `C:\msys64\mingw64\bin` to my path, nothing else. What should I be looking for? – blundered_bishop Apr 04 '22 at 08:04
  • try using [dependency walker](https://github.com/lucasg/Dependencies) to see which DLLs your executable requires – Alan Birtles Apr 04 '22 at 08:08
  • @AlanBirtles this is what i get: https://imgur.com/kOntFZa . Sorry but I'm not quite sure what to look for. – blundered_bishop Apr 04 '22 at 08:14
  • you need to make sure the gcc dlls are available on your path. Your application seems to be crashing or not starting, you need to use a debugger or at least look at the return code of your application to find out why – Alan Birtles Apr 04 '22 at 08:19
  • Perhaps related? https://stackoverflow.com/questions/57208442/c-program-using-filesystem-library-just-does-nothing-on-windows – orhtej2 Apr 04 '22 at 08:26
  • @orhtej2 that seems to be it. I've reproduced the steps the answer suggests and `cygpath -w /mingw64` returns a path from the python distribution `anaconda`. So I guess @HolyBlackCat was right, something is wrong with the path. I'll look into it and eventually update the question. – blundered_bishop Apr 04 '22 at 08:41

0 Answers0