我正在尝试在Windows上运行一个使用<filesystem>
库的程序。我正在使用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)
可能出了什么问题?
如果我注释掉带有 std::filesystem::current_path()
的行,则可以获得 Hello World
输出并且 $?
是 0
,这说明编译器正常工作。
current_path
可能会抛出异常。请参见此处:https://en.cppreference.com/w/cpp/filesystem/current_path - Michael Mahn