I tried to use the C++ version of Pytorch (version 2.0) i.e. LibTorch on win11, but when I do the mingw32-make operation I get a missing file error as follows:
example directory is laid out like this:
example-app/
build
CMakeLists.txt
example-app.cpp
Detailed Output of running cmake .. -G "MinGW Makefiles" .
:
(base) PS D:\pytorch-cpp\pytorch-test\example-app\build> cmake .. -G "MinGW Makefiles" .
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/MinGW/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Torch: D:/pytorch-cpp/pytorch-cpu/libtorch/lib/torch.lib
-- Pytorch status:
-- libraries: torch;torch_library;D:/pytorch-cpp/pytorch-cpu/libtorch/lib/c10.lib;D:/pytorch-cpp/pytorch-cpu/libtorch/lib/kineto.lib
-- Configuring done
-- Generating done
-- Build files have been written to: D:/pytorch-cpp/pytorch-test/example-app/build
(base) PS D:\pytorch-cpp\pytorch-test\example-app\build> mingw32-make
[ 50%] Building CXX object CMakeFiles/app.dir/example-app.cpp.obj
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: /wd4522: 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
mingw32-make[2]: *** [CMakeFiles/app.dir/example-app.cpp.obj] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/app.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:89: recipe for target 'all' failed
mingw32-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 is:
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} ]
Building PyTorch with MinGW is not supported at the time of this writing. You can switch to using a Visual Studio CMake generator instead.
See https://github.com/pytorch/pytorch/issues/24460#issuecomment-1383277152.
Note: I can't actually tell why in particular you're getting these flags added. I grepped through the PyTorch repo, and not all the flags you're getting on your target are even mentioned in the PyTorch repo. Some are (see https://github.com/pytorch/pytorch/blob/main/CMakeLists.txt#L894), but they way they're specified there (CMAKE_<LANG>_FLAGS
) shouldn't propagate to link-dependent targets of any PyTorch targets, and they're even specified in a control-flow block that only gets entered if the condition MSVC
is truthy, which is the correct way to write what it's doing there (a wrong way would be to use WIN32
). ¯\_(ツ)_/¯