使用库C++程序在<filesystem>Windows上什么都不做
C++ Program using <filesystem> Library just does nothing on Windows
我正在尝试使用Windows上运行的<filesystem>
库获取程序。我正在使用MSYS2的(64位)g++
。
#include <iostream>
#include <filesystem>
int main()
{
std::cout << "Hello Worldn";
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:msys64mingw64bing++.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
输出,如果我用std::filesystem::current_path()
调用注释掉该行,则$?
0
,因此编译器正在工作。
感谢评论者放弃了一些想法,最终导致我找到了解决方案。
TL;博士
如果您安装了各种基于 MinGW 的工具集(例如Cygwin、MinGW、MSYS、Git Bash),请使用该集附带的 shell。只需仔细添加mingw*/bin
路径PATH
;而是坚持使用相应外壳的默认PATH
设置。单击此处,如果您想为MSYS外壳提供方便的上下文菜单快捷方式。
关于百万兆瓦的故事
问题是由于我奇怪的本地设置。随着时间的流逝,我在我的计算机上积累了各种版本的MinGW。如果我需要一个实用程序,比如说diff
,我会谷歌它并从某个地方(通常是sourceforge)下载一些东西,将其安装到某个地方并将bin
文件夹添加到我的PATH
中。这种策略适用于大多数事情。
我随着时间的推移安装的工具集,包括但不限于:
- 明吉瓦(32和64)
- 天鹅座
- MSYS
- Git (+ Git Bash)
很长一段时间以来,我一直在使用Git Bash来处理所有事情。最近我想获得最新的g++
编译器和其他东西,发现MSYS2有一个包管理器(pacman
)和最新的包,所以我安装了它。并将其添加到PATH
中,然后使用Git Bash。
但是,每个带有自己的shell的工具集都有自己的PATH
魔力,并具有自己的MinGW副本。所以我让我的 Git Bash 使用MSYS2g++
,但从 Git Bash 执行的应用程序仍然使用GitBash的MinGW副本中的 DLL。我不确定,但我认为这很可能是问题所在。
为了找出答案,我在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 FilesGitmingw64
MSYS2带有自己的shell,可以正确设置其PATH
,以便应用程序正常工作。在MSYS2外壳中:
$ ./a.exe
Hello World
Current path is "E:\temporary\2019_07_25-gpp_filesystem_test"
对于不太可能发生的事件,有人遇到了类似的麻烦,并希望在各种文件夹中更轻松地使用 MSYS2 shell,请查看此存储库,其中包含一个reg
脚本,为MSYS2shell 设置了方便的上下文菜单快捷方式:https://github.com/njzhangyifei/msys2-mingw-shortcut-menus
- 我需要安装什么才能获得在Windows Server 2012 R2 Standard上运行C++hello world
- 我应该使用什么信号来终止/终止Windows上的应用程序
- 什么是"MFC Dialog Extension"?(Windows/C++ 新手 - Linux 背景)
- Windows(Visual Studio)中的-fpermssive标志的替代方案是什么
- 在Windows Server 2012上运行VS2019编译的应用程序需要什么?
- 使用库C++程序在<filesystem>Windows上什么都不做
- 有什么方法可以用本机C 代码替换Windows API计时器(settimer)
- 有什么方法可以改变同步Windows API SendARP的行为吗?
- 什么是窗口最大化/最小化/恢复的WM Windows消息
- 什么是通用运行时组件 #ifdef 适用于Windows(UWP)而不是iOS
- "Windows doesn't support proper dynamic linking?"是什么意思
- 测试使用visual studio编写的C++android应用程序的最佳方法是什么.不升级到windows专业版
- 将Objective-C应用程序移植到Windows.什么是最好的选择
- 让我的Windows桌面程序防篡改或防篡改的3-5种方法是什么
- 将gstreamer合并到Qt(Windows)的正确方法是什么?
- 在Windows上运行cygwin的C ++程序的安装后过程是什么
- Windows的unicode数据类型使用什么unicode编码(UTF-8、UTF-16等)
- Windows上的模板有什么问题?解决方案是什么
- Windows桌面的表现最佳的计时器是什么?
- 确定 Windows Server 2003 SP 1 是 32 位还是 64 位的正确方法是什么?