pytorch C++ version compiles with Cmake error in windows 11

As the title, I tried to use the C++ version of Pytorch i.e. LibTorch on win11, but when I do the mingw32-make operation I get a missing file error as follows:

[ 50%] Building CXX object CMakeFiles/app.dir/example-app.cpp.obj
g++.exe: error: /Z7: No such file or directory
g++.exe: error: /EHsc: No such file or directory
g++.exe: error: /DNOMINMAX: No such file or directory
g++.exe: error: /wd4267: No such file or directory
g++.exe: error: /wd4251: No such file or directory
g++.exe: error: /wd4838: No such file or directory
g++.exe: error: /wd4305: No such file or directory
g++.exe: error: /wd4244: No such file or directory
g++.exe: error: /wd4190: No such file or directory
g++.exe: error: /wd4101: No such file or directory
g++.exe: error: /wd4996: No such file or directory
g++.exe: error: /wd4275: No such file or directory
g++.exe: error: /bigobj: No such file or directory
CMakeFiles\app.dir\build.make:75: recipe for target 'CMakeFiles/app.dir/example-app.cpp.obj' failed
make[2]: *** [CMakeFiles/app.dir/example-app.cpp.obj] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/app.dir/all' failed
make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:89: recipe for target 'all' failed
make: *** [all] Error 2

I built the project exactly as instructed on the official website, where example-app.cpp is

#include <iostream>
#include <torch/torch.h>
int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

CMakeLists.txt as:

cmake_minimum_required(VERSION 3.0)
project(example-app)

list(APPEND CMAKE_PREFIX_PATH "D:/pytorch-cpp/pytorch-cpu/libtorch")
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
    message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status:")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")

add_executable(app example-app.cpp)
target_link_libraries(app ${TORCH_LIBRARIES})
set_property(TARGET app PROPERTY CXX_STANDARD 11)
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET cpp
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:cpp>)
endif (MSVC)

I tried multiple methods to compile successfully, they all report the same error, even using Win7 and Linux, where Win7 has the same error as Win11, but it compiles fine on Linux. How should I solve this problem on Windows system? The correct result of the program should be:

0.2063  0.6593  0.0866
0.0796  0.5841  0.1569
[ Variable[CPUFloatType]{2,3} ]

Read more here: Source link