Hello,
I have a problem building minimal C++ example provided in the official docs (https://pytorch.org/cppdocs/installing.html#minimal-example) with Libtorch version 1.5.0 using VS 2015 C++ build tools (v140). I’m using the pre-built pytorch binaries downloaded from the official site.
Minimal example app
example-app.cpp
#include <torch/torch.h>
#include <iostream>
int main() {
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
Commands to build the example
mkdir build
cmake -G "Visual Studio 16" -A x64 -T v140 -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build build --config Release
I got bunch of warnings and errors complaining about constexpr
:
Warnings:
warning C4814: in C++14 'constexpr' will not imply 'const'; consider explicitly specifying 'const'
Errors: (selecting first few errors)
libtorch\include\c10/util/ConstexprCrc.h(103): error C3250: 'i': declaration is not allowed in 'constexpr' function body
libtorch\include\c10/util/ConstexprCrc.h(103): error C3249: illegal statement or sub-expression for 'constexpr' function
libtorch\include\c10/util/Optional.h(277): error C2556: 'c10::ScalarType &c10::optional<c10::ScalarType>::contained_val(void) const &': overloaded function differs only by return type from 'const c10::ScalarType &c10::optional<c10::ScalarType>::contained_val(void) const &'
...
I was able to compile this minimal example using newer VS toolsets (e.g. v142). Unfortunately, I need to use v140.
Using older Pytorch versions (v1.3.1) however did not result in any errors. Using the version v1.4.0 there is only one error: error C2872: 'std': ambiguous symbol
, which I was able to solve prepending std
with ::
as suggested in Compiler report "'std': ambiguous symbol" error under Visual C++ 2017.
Could this be a bug in the actual Libtorch version or has Libtorch abandoned support for toolset v140?
My environment:
Windows 10
Compiler: MSVC 19.0.24245.0
CUDA: v9.2
I will appreciate any help.
Thanks,
Matus