简体   繁体   English

C ++程序使用 <filesystem> 库在Windows上什么都不做

[英]C++ Program using <filesystem> Library just does nothing on Windows

I'm trying to get a program using the <filesystem> library running on Windows. 我正在尝试使用Windows上运行的<filesystem>库获取程序。 I'm using MSYS2's (64 Bit) g++ . 我正在使用MSYS2的(64位) g++

#include <iostream>
#include <filesystem>

int main()
{
    std::cout << "Hello World\n";
    std::cout << "Current path is " << std::filesystem::current_path() << '\n';
}

I'm building it with 我正在建立它

g++ -std=c++17 -Wall -Werror -Wextra main.cpp -lstdc++fs

I get no console output from the compiler. 我没有从编译器获得任何控制台输出。 It silently generates an a.exe . 它默默地生成一个a.exe Executing the a.exe just does nothing. 执行a.exe只会执行任何操作。 There is no output and no errors. 没有输出,也没有错误。 $? (which is said to contain a program's return code) is 127 after running the program. (据说包含程序的返回代码)在运行该程序后为127

g++ -v prints: g++ -v打印:

Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-9.1.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++ --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --enable-plugin --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev3, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 9.1.0 (Rev3, Built by MSYS2 project)

What could be wrong? 有什么事吗

(I get a Hello World output and $? is 0 , if I comment out the line with the std::filesystem::current_path() call, so the compiler is working.) (如果我通过std::filesystem::current_path()调用注释掉了这一行,则会得到Hello World输出,并且$?0 ,因此编译器可以正常工作。)

Thanks to the commenters for dropping some ideas that finally led me to finding a solution. 感谢评论者放弃了一些想法,最终使我找到了解决方案。

TL;DR TL; DR

If you have various MinGW -based tool sets installed (eg Cygwin , MinGW , MSYS , Git Bash ), use the shell shipped with the set. 如果您安装了各种基于MinGW的工具集(例如CygwinMinGWMSYSGit Bash ),请使用该工具包随附的外壳。 Only carefully add the mingw*/bin paths to PATH ; 只需要小心地将mingw*/bin路径添加到PATH rather stick to the default PATH setup of the respective shell. 而是坚持使用各自外壳程序的默认PATH设置。 Click here, if you want to have convenient context menu shortcuts for the MSYS shell . 如果要使用MSYS Shell的便捷上下文菜单快捷方式,请单击此处


A Story about a million MinGW s 关于一百万个MinGW的故事

The problem was due to my weird local setup. 问题是由于我的本地设置很奇怪。 Over the time I accumulated various versions of MinGW spread all over my computer. 随着时间的流逝,我在计算机上累积了各种版本的MinGW If I needed a utility, say diff , I would google it and download something from somewhere (often sourceforge ), install it to somewhere and add the bin folder to my PATH . 如果我需要一个实用程序,例如diff ,我会用它搜索并从某个地方(通常是sourceforge )下载一些东西,将其安装到某个地方,然后将bin文件夹添加到我的PATH This strategy worked for most things. 此策略适用于大多数情况。

Toolsets I've installed over the time, including but not restricted to: 我过去安装的工具集,包括但不限于:

  • MinGW (32 and 64) MinGW(32和64)
  • Cygwin Cygwin的
  • MSYS MSYS
  • Git (+ Git Bash) Git(+ Git Bash)

For some long time now I have mostly been using the Git Bash for everything. 很长一段时间以来,我大部分时间都在使用Git Bash Lately I wanted to get the newest g++ compiler and stuff and found that MSYS2 has a package manager ( pacman ) and pretty up to date packages so I installed it. 最近,我想获得最新的g++编译器和内容,并发现MSYS2具有一个程序包管理器( pacman )和最新的程序包,因此我安装了它。 And added it to the PATH , then used the Git Bash . 并将其添加到PATH ,然后使用Git Bash

However, each toolset coming with its own shell does its own PATH magic and has its own MinGW copy. 但是,每个带有自己的外壳的工具集都有自己的PATH魔术,并拥有自己的MinGW副本。 So I made my Git Bash use the MSYS2 g++ , but applications executed from the Git Bash still used DLLs from Git Bash 's MinGW copy. 因此,我使Git Bash使用MSYS2 g++ ,但是从Git Bash执行的应用程序仍然使用Git BashMinGW副本中的DLL。 I don't know for sure, but I think it's very probably that this was the problem. 我不确定,但我认为这很可能就是问题所在。

For finding out, I used the following in the Git Bash : 为了找出答案,我在Git Bash中使用了以下内容:

# because of my PATH adjustment, I got the right g++
$ which g++
/c/msys64/mingw64/bin/g++

# my program uses DLLs from /mingw64, though
$ ldd a.exe
        ntdll.dll => /c/Windows/SYSTEM32/ntdll.dll (0x77060000)
        kernel32.dll => /c/Windows/system32/kernel32.dll (0x76e40000)
        KERNELBASE.dll => /c/Windows/system32/KERNELBASE.dll (0x7fefcc00000)
        msvcrt.dll => /c/Windows/system32/msvcrt.dll (0x7fefcf70000)
        libgcc_s_seh-1.dll => /mingw64/bin/libgcc_s_seh-1.dll (0x61440000)
        libwinpthread-1.dll => /mingw64/bin/libwinpthread-1.dll (0x64940000)
        libstdc++-6.dll => /mingw64/bin/libstdc++-6.dll (0x6fc40000)
        USER32.dll => /c/Windows/system32/USER32.dll (0x76f60000)
        GDI32.dll => /c/Windows/system32/GDI32.dll (0x7fefddb0000)
        LPK.dll => /c/Windows/system32/LPK.dll (0x7fefd540000)
        USP10.dll => /c/Windows/system32/USP10.dll (0x7fefdbe0000)

# with cygpath I can find out that this is actually the Git Bash's installation
$ cygpath -w /mingw64
C:\Program Files\Git\mingw64

MSYS2 comes with its own shell, which sets up its PATH correctly so the application works. MSYS2带有自己的外壳程序,该外壳程序可以正确设置其PATH ,以便应用程序正常工作。 In the MSYS2 shell: MSYS2 Shell中:

$ ./a.exe
Hello World
Current path is "E:\\temporary\\2019_07_25-gpp_filesystem_test"

For the unlikely event someone got into a similar trouble and would like to use the MSYS2 shell more easily in various folders, check out this repo with a reg script setting up convenient context menu shortcuts for the MSYS2 shell: https://github.com/njzhangyifei/msys2-mingw-shortcut-menus 对于不太可能发生的事件,有人遇到了类似的麻烦,想更轻松地在各种文件夹中使用MSYS2 shell,请使用reg脚本查看此仓库 ,为registry设置MSYS2 shell的便捷上下文菜单快捷方式: https : //github.com / njzhangyifei / msys2-的mingw的快捷菜单

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

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