如何在Windows上使用带有mingw32-make和msys2的std::filesystem



我正在尝试在Windows上编译我的Linux代码。它因为我使用了std::filesystem而阻塞。所以我尝试根据这篇文章做一个最小的例子:

在Windows 10上使用nuwen MinGW的C++17文件系统

我使用相同的代码:

#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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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]: *** [CMakeFilesfilesystem.dirbuild.make:62: CMakeFiles/filesystem.dir/main.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFilesMakefile2: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

来自 set(CMAKE_VERBOSE_MAKEFILE ON( 的日志:

mingw32-make.exe
"C:Program FilesCMakebincmake.exe" -SC:UsersGuillaumeDocumentsdevC++testfilesystem -BC:UsersGuillaumeDocumentsdevC++testfilesystembuild --check-build-system CMakeFilesMakefile.cmake 0
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build
"C:Program FilesCMakebincmake.exe" -E cmake_progress_start C:UsersGuillaumeDocumentsdevC++testfilesystembuildCMakeFiles C:UsersGuillaumeDocumentsdevC++testfilesystembuildCMakeFilesprogress.marks
C:/tools/msys64/mingw64/bin/mingw32-make -f CMakeFilesMakefile2 all
mingw32-make[1]: Entering directory 'C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build'
C:/tools/msys64/mingw64/bin/mingw32-make -f CMakeFilesfilesystem.dirbuild.make CMakeFiles/filesystem.dir/depend
mingw32-make[2]: Entering directory 'C:/Users/Guillaume/Documents/dev/C++/test/filesystem/build'
"C:Program FilesCMakebincmake.exe" -E cmake_depends "MinGW Makefiles" C:UsersGuillaumeDocumentsdevC++testfilesystem C:UsersGuillaumeDocumentsdevC++testfilesystem C:UsersGuillaumeDocumentsdevC++testfilesystembuild C:UsersGuillaumeDocumentsdevC++testfilesystembuild C:UsersGuillaumeDocumentsdevC++testfilesystembuildCMakeFilesfilesystem.dirDependInfo.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 CMakeFilesfilesystem.dirbuild.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:ProgramDatachocolateybing++.exe    -std=c++17 -o CMakeFilesfilesystem.dirmain.cpp.obj -c C:UsersGuillaumeDocumentsdevC++testfilesystemmain.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:UsersGuillaumeDocumentsdevC++testfilesystemmain.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。这可能是问题所在...但是我能做什么?

问题来自 cmake 使用的 gcc 版本。 为了使用正确的版本,我更改了环境路径的顺序(系统>高级系统参数->环境变量>路径(,并将"C:\tools\msys64\mingw64\bin"放在顶部。最后,我从我的系统环境中抑制了可能使 msys2 gcc 路径过载的路径。

希望它可以帮助别人。

最新更新