Problems compiling with Libtorch v1.5.0 and VS toolset v140

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

1 Like

I guess you can try to remove the constexprs as reported in the error log.

Could this be a bug in the actual Libtorch version or has Libtorch abandoned support for toolset v140?

We didn’t test the support for toolset v140. And there’s no plan for maintaining its support given the fact the binaries are compatible across v140 to v142.

Hello, thanks for the reply.

I tried, but other errors showed up, namely in header files Optional.h, ScalarType.h and others.

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 &'
libtorch\include\c10/util/Optional.h(277): error C2373: 'c10::optional<c10::ScalarType>::contained_val': redefinition; different type modifiers
...

I think the changes, that caused my problems were introduced in (https://github.com/pytorch/pytorch/pull/30916, https://github.com/pytorch/pytorch/pull/30919 and probably others).

Correct me if I’m wrong, but that means there is requirement for C++14 compiler in version 1.5.0, but toolset v140 does not support entire standard --> extended constexpr (https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019). So for the version 1.5.0 and future versions I have to use the newer toolset than v140?