我正在尝试在 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 编译我的代码?
编辑:错误的开始(对于stackoverflow来说太大)
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
来自 set(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。这可能是问题......但我能做什么?