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

标签 c++ windows g++ msys

我正在尝试使用 <filesystem> 获取程序在 Windows 上运行的库。我正在使用 MSYS2(64 位)g++ .

#include <iostream>
#include <filesystem>

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

我正在构建它

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

我没有从编译器得到控制台输出。它默默地生成一个 a.exe .执行 a.exe什么都不做。没有输出,也没有错误。 $? (据说包含程序的返回码)是127运行程序后。

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)

有什么问题吗?

(我得到 Hello World 输出并且 $?0 ,如果我用 std::filesystem::current_path() 调用注释掉该行,那么编译器正在工作。)

最佳答案

感谢评论者提出的一些想法,最终让我找到了解决方案。

长话短说

如果您安装了各种基于 MinGW 的工具集(例如 CygwinMinGWMSYS、< em>Git Bash),使用套件附带的 shell。小心地将 mingw*/bin 路径添加到 PATH;而是坚持各自 shell 的默认 PATH 设置。 Click here, if you want to have convenient context menu shortcuts for the MSYS shell .


关于一百万 MinGW 的故事

问题是由于我奇怪的本地设置。随着时间的推移,我在我的计算机上积累了各种版本的 MinGW。如果我需要一个实用程序,比如说 diff,我会用谷歌搜索它并从某个地方下载一些东西(通常是 sourceforge),将它安装到某个地方并添加 bin 文件夹到我的 PATH。这种策略适用于大多数情况。

我一直以来安装的工具集,包括但不限于:

  • MinGW(32 和 64)
  • 赛格文
  • 管理系统
  • Git(+ Git Bash)

很长一段时间以来,我大部分时间都在使用 Git Bash 来处理所有事情。最近我想获得最新的 g++ 编译器和东西,发现 MSYS2 有一个包管理器 (pacman) 和非常新的包所以我安装了它。并将其添加到 PATH,然后使用 Git Bash

但是,每个带有自己 shell 的工具集都有自己的 PATH 魔法,并且有自己的 MinGW 拷贝。所以我让我的 Git Bash 使用 MSYS2 g++,但是从 Git Bash 执行的应用程序仍然使用来自Git BashMinGW 拷贝。我不确定,但我认为这很可能就是问题所在。

为了找到答案,我在 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 带有自己的外壳,可以正确设置其 PATH 以便应用程序正常运行。在 MSYS2 外壳中:

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

万一有人遇到类似的麻烦并希望在各种文件夹中更轻松地使用 MSYS2 shell,请使用 reg 脚本设置查看此 repo为 MSYS2 shell 设置方便的上下文菜单快捷方式:https://github.com/njzhangyifei/msys2-mingw-shortcut-menus

关于使用 <filesystem> 库的 C++ 程序在 Windows 上什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208442/

相关文章:

c++ - 高效分组重叠矩形

c++ - OpenGL 体素引擎人脸合并与逐顶点环境光遮挡

c++ - 将 return 替换为标准输入的选项卡

c++ - c++和g++如何处理unicode?

c++ - 赋值运算符的要求

c++ - 未处理的异常错误

c# - 如何将 c# uwp 应用程序设置为默认应用程序以打开某些文件?

java - Windows 密码和组

windows - 在 Golang 中使用 Exec 时如何隐藏命令提示符窗口?

c++ - 大数据结构上的缓存行检索和性能