如何在 Windows 上将 std::filesystem 与 mingw32-make 和 msys2 一起使用
Posted
技术标签:
【中文标题】如何在 Windows 上将 std::filesystem 与 mingw32-make 和 msys2 一起使用【英文标题】:how can I use std::filesystem with mingw32-make and msys2 on windows 【发布时间】:2020-01-23 13:11:45 【问题描述】:我正在尝试在 Windows 上编译我的 linux 代码。由于我使用了 std::filesystem,它被阻塞了。所以我尝试根据这篇文章做一个最小的例子:
C++17 filesystem using nuwen MinGW on Windows 10
我使用相同的代码:
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
std::cout << "Current path is " << fs::current_path() << '\n';
我使用 msys2。好的,当我执行 g++ -std=c++17 ../main.cpp 时,效果很好:
当前路径是“C:\Users\Guillaume\Documents\dev\C++\test\filesystem\build”
但我的项目肯定太大而不能仅与 g++ 一起使用。所以我使用 cmake 和 mingw32-make。但是,它不起作用,我的错误与上面的帖子相同。补充资料:
g++ --version
g++.exe (Rev2, Built by MSYS2 project) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
和
mingw32-make.exe --version
GNU Make 4.2.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
我的问题:我应该怎么做才能在 msys2 下使用 mingw32-make 编译我的代码?
编辑:错误的开始(对于***来说太大)
mingw32-make.exe
[ 50%] Building CXX object CMakeFiles/filesystem.dir/main.cpp.obj
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:2:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
|| (__p.has_root_name() && __p.root_name() != root_name()))
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iosfwd:40,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:38,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:1:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/postypes.h:221:5: note: candidate: 'template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)'
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^~~~~~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/postypes.h:221:5: note: template argument deduction/substitution failed:
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:2:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:60: note: 'std::filesystem::__cxx11::path' is not derived from 'const std::fpos<_StateT>'
|| (__p.has_root_name() && __p.root_name() != root_name()))
^
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:64,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:39,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:40,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:1:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_pair.h:456:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)'
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_pair.h:456:5: note: template argument deduction/substitution failed:
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:2:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:60: note: 'std::filesystem::__cxx11::path' is not derived from 'const std::pair<_T1, _T2>'
|| (__p.has_root_name() && __p.root_name() != root_name()))
^
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:67,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:39,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:40,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:1:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:311:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)'
operator!=(const reverse_iterator<_Iterator>& __x,
^~~~~~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:311:5: note: template argument deduction/substitution failed:
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:2:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:60: note: 'std::filesystem::__cxx11::path' is not derived from 'const std::reverse_iterator<_Iterator>'
|| (__p.has_root_name() && __p.root_name() != root_name()))
^
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:67,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:39,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:40,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:1:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:349:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)'
operator!=(const reverse_iterator<_IteratorL>& __x,
^~~~~~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:349:5: note: template argument deduction/substitution failed:
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:2:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:60: note: 'std::filesystem::__cxx11::path' is not derived from 'const std::reverse_iterator<_Iterator>'
|| (__p.has_root_name() && __p.root_name() != root_name()))
^
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:67,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:39,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:40,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:1:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:1124:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)'
operator!=(const move_iterator<_IteratorL>& __x,
...
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:180:7: note: template argument deduction/substitution failed:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:174:5: note: candidate: 'std::filesystem::__cxx11::path::path(std::filesystem::__cxx11::path::string_type&&, std::filesystem::__cxx11::path::format)'
path(string_type&& __source, format = auto_format)
^~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:174:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path::string_type&&' aka 'std::__cxx11::basic_string<wchar_t>&&'
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: candidate: 'std::filesystem::__cxx11::path::path(std::filesystem::__cxx11::path&&)'
path(path&& __p) noexcept
^~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path&&'
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: candidate: 'std::filesystem::__cxx11::path::path(const std::filesystem::__cxx11::path&)'
path(const path& __p) = default;
^~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'const std::filesystem::__cxx11::path&'
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate: 'std::filesystem::__cxx11::path::path()'
path() noexcept
^~~~
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate expects 0 arguments, 1 provided
mingw32-make[2]: *** [CMakeFiles\filesystem.dir\build.make:62: CMakeFiles/filesystem.dir/main.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:75: CMakeFiles/filesystem.dir/all] Error 2
mingw32-make: *** [Makefile:83: all] Error 2
和 CMakeLists.txt :
cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project (filesystem)
add_executable(filesystem main.cpp)
target_link_libraries(filesystem stdc++fs )
编辑 2:
我修改了环境路径,把C:\tools\msys64放在最上面。
我用的是cmake-gui,配置的日志是:
The C compiler identification is GNU 8.1.0
The CXX compiler identification is GNU 8.1.0
Check for working C compiler: C:/ProgramData/chocolatey/bin/gcc.exe
Check for working C compiler: C:/ProgramData/chocolatey/bin/gcc.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: C:/ProgramData/chocolatey/bin/g++.exe
Check for working CXX compiler: C:/ProgramData/chocolatey/bin/g++.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Configuring done
集合中的日志(CMAKE_VERBOSE_MAKEFILE ON):
mingw32-make.exe
"C:\Program Files\CMake\bin\cmake.exe" -SC:\Users\Guillaume\Documents\dev\C++\test\filesystem -BC:\Users\Guillaume\Documents\dev\C++\test\filesystem\build --check-build-system CMakeFiles\Makefile.cmake 0
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\Guillaume\Documents\dev\C++\test\filesystem\build\CMakeFiles C:\Users\Guillaume\Documents\dev\C++\test\filesystem\build\CMakeFiles\progress.marks
C:/tools/msys64/mingw64/bin/mingw32-make -f CMakeFiles\Makefile2 all
mingw32-make[1]: Entering directory 'C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build'
C:/tools/msys64/mingw64/bin/mingw32-make -f CMakeFiles\filesystem.dir\build.make CMakeFiles/filesystem.dir/depend
mingw32-make[2]: Entering directory 'C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Users\Guillaume\Documents\dev\C++\test\filesystem C:\Users\Guillaume\Documents\dev\C++\test\filesystem C:\Users\Guillaume\Documents\dev\C++\test\filesystem\build C:\Users\Guillaume\Documents\dev\C++\test\filesystem\build C:\Users\Guillaume\Documents\dev\C++\test\filesystem\build\CMakeFiles\filesystem.dir\DependInfo.cmake --color=
Scanning dependencies of target filesystem
mingw32-make[2]: Leaving directory 'C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build'
C:/tools/msys64/mingw64/bin/mingw32-make -f CMakeFiles\filesystem.dir\build.make CMakeFiles/filesystem.dir/build
mingw32-make[2]: Entering directory 'C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build'
[ 50%] Building CXX object CMakeFiles/filesystem.dir/main.cpp.obj
C:\ProgramData\chocolatey\bin\g++.exe -std=c++17 -o CMakeFiles\filesystem.dir\main.cpp.obj -c C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp
In file included from C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
from C:\Users\Guillaume\Documents\dev\C++\test\filesystem\main.cpp:2:
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
|| (__p.has_root_name() && __p.root_name() != root_name()))
...
...
我的 CMAKE_CXX_COMPILER 仍然是 C:/ProgramData/chocolatey/bin/g++.exe。这可能是问题所在......但是我该怎么办?
【问题讨论】:
使用这些工具为 Windows 编程无论如何都不会让你走得太远。 @MichaelChourdakis 为什么不呢?你能详细说明一下吗? This thread 声称链接帖子中提到的错误已在 GCC 9 中修复。您能否发布您收到的确切错误消息和minimal reproducible example? @MichaelChourdakis 不使用 Visual Studio 的一个原因是 Microsoft 不提供遵循 C++ 标准的 C++ 编译器。您可以使用 Qt 和 Qt Creator 或 Clion 实现相同的目的。 @MichaelChourdakis “绝对没有理由不使用 Visual Studio。” 在我们公司有这样的原因,我看到多家公司都有相同的原因。跨度> 【参考方案1】:问题来自cmake使用的gcc版本。 为了使用正确的版本,我更改了环境路径的顺序(系统->高级系统参数->环境变量->路径)并将“C:\tools\msys64\mingw64\bin”放在顶部。最后,我从我的系统环境中抑制了可能使 msys2 gcc 路径过载的路径。
希望它可以帮助别人。
【讨论】:
以上是关于如何在 Windows 上将 std::filesystem 与 mingw32-make 和 msys2 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Windows 10 上将代理配置到 GitBash 环境中
如何在 Windows 上将 Qt QString 转换为 LPCTSTR
如何在 Windows 上将 NPM 升级到特定版本 6.4.1?
如何在 Windows 10 上将 Xdebug 3 连接到 PhpStorm?