CMake error and fatal error LNK1181 building from source on Windows 10

username: Bega
cooked: <p>I’m getting the following error while compiling PyTorch from source:</p>
<pre><code class="lang-auto">[1818/2610] Linking CXX shared library bin\caffe2.dll
FAILED: bin/caffe2.dll lib/caffe2.lib
cmd.exe /C "cd . &amp;&amp; "C:\Program Files (x86)\CMake\bin\cmake.exe" -E vs_link_dll --intdir=caffe2\CMakeFiles\caffe2.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100177~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100177~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1411~1.255\bin\HostX64\x64\link.exe /nologo @CMakeFiles\caffe2.rsp  /out:bin\caffe2.dll /implib:lib\caffe2.lib /pdb:bin\caffe2.pdb /dll /version:0.0 /INCREMENTAL:NO   &amp;&amp; cd ."
LINK: command "C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1411~1.255\bin\HostX64\x64\link.exe /nologo @CMakeFiles\caffe2.rsp /out:bin\caffe2.dll /implib:lib\caffe2.lib /pdb:bin\caffe2.pdb /dll /version:0.0 /INCREMENTAL:NO /MANIFEST /MANIFESTFILE:bin\caffe2.dll.manifest" failed (exit code 1181) with the following output:
LINK : fatal error LNK1181: cannot open input file 'Files/pytorch/build/lib/caffe2_protos.lib'
ninja: build stopped: subcommand failed.
</code></pre>
<p>Full-log:<br>
<a href="https://github.com/GibranBenitez/temporal/blob/master/ninja_build" rel="nofollow noopener">ninja_build_full-log</a></p>
<p>This happened when I followed this solution:<br>
<a href="https://discuss.pytorch.org/t/pytorch-build-from-source-on-windows/40288">pytorch_discussion</a></p>
<p>Previously I have tried to install it from source using VS as mentioned in the README, but I encountered a CMake error:</p>
<pre><code class="lang-auto">CMake Error at caffe2_gpu_generated_THCTensor.cu.obj.Release.cmake:219
</code></pre>
<p>And now when I tried to use the same process I get the following error:</p>
<pre><code class="lang-auto">-- Build files have been written to: C:/Program Files/pytorch/build
ninja: error: unknown target '/maxcpucount:8'
</code></pre>
<p>Full-log:<br>
<a href="https://github.com/GibranBenitez/temporal/blob/master/VS_cmake_full-log" rel="nofollow noopener">VS_full-log</a></p>
<p>Could you please help me with this issue?</p>
<p>My setup:</p>
<p>OS: Microsoft Windows 10<br>
CMake version: 3.14.5<br>
Python 3.6.1 :: Anaconda custom (64-bit)<br>
nvcc: NVIDIA ® Cuda compiler driver<br>
Copyright © 2005-2018 NVIDIA Corporation<br>
Built on Sat_Aug_25_21:08:04_Central_Daylight_Time_2018<br>
Cuda compilation tools, release 10.0, V10.0.130<br>
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin\cudnn64_7.dl<br>
<a href="https://github.com/GibranBenitez/temporal/blob/master/Path" rel="nofollow noopener">My env-var PATH:</a></p>

username: Bega
cooked: <p><a class="mention" href="/u/peterjc123">@peterjc123</a> could you please help me with this?</p>

username: peterjc123
cooked: <p>Hey, would you please get a fresh clone and do that again? Looks like you configured with Ninja at first and turned it off later.</p>

username: peterjc123
cooked: <p>BTW, could you please send me the <code>ninja.build</code> file in the <code>build</code> directory?</p>

username: peterjc123
cooked: <p>Well, I saw that your build directory contains space. As a workground, you can just avoid that to make build pass. However, more details to fix this issue are welcomed.</p>

username: Bega
cooked: <p>Thank you for your quick response.<br>
I changed the build directory and tried again. However another error appears:</p>
<pre><code class="lang-auto">C:\pytorch\torch/csrc/jit/tracer.h(314): warning C4273: 'torch::jit::tracer::addInputs': inconsistent dll linkage [C:\p
ytorch\build\caffe2\torch\torch_python.vcxproj]
  C:\pytorch\torch/csrc/jit/tracer.h(299): note: see previous definition of 'addInputs'
C:\pytorch\torch/csrc/jit/tracer.h(321): warning C4273: 'torch::jit::tracer::addInputs': inconsistent dll linkage [C:\p
ytorch\build\caffe2\torch\torch_python.vcxproj]
  C:\pytorch\torch/csrc/jit/tracer.h(305): note: see previous definition of 'addInputs'
  serialization.cpp
C:\pytorch\torch/csrc/jit/tracer.h(314): warning C4273: 'torch::jit::tracer::addInputs': inconsistent dll linkage [C:\p
ytorch\build\caffe2\torch\torch_python.vcxproj]
  C:\pytorch\torch/csrc/jit/tracer.h(299): note: see previous definition of 'addInputs'
C:\pytorch\torch/csrc/jit/tracer.h(321): warning C4273: 'torch::jit::tracer::addInputs': inconsistent dll linkage [C:\p
ytorch\build\caffe2\torch\torch_python.vcxproj]
  C:\pytorch\torch/csrc/jit/tracer.h(305): note: see previous definition of 'addInputs'
     Creating library C:/pytorch/build/lib/Release/torch_python.lib and object C:/pytorch/build/lib/Release/torch_pytho
  n.exp
init.cpp.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject THPDeviceTy
pe) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_arg_parser.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject TH
PDeviceType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
tensor_new.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject THPDevice
Type) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
Event.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject THPDeviceType)
 imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject THPDeviceTy
pe) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_variable_methods.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobj
ect THPDeviceType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_torch_functions.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobje
ct THPDeviceType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_nn_functions.obj : warning LNK4049: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject
THPDeviceType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_sugared_value.obj : warning LNK4217: locally defined symbol ?THPDeviceType@@3U_typeobject@@A (struct _typeobject
 THPDeviceType) imported in function "void __cdecl torch::jit::script::`dynamic initializer for 'empty_trees''(void)" (
??__Eempty_trees@script@jit@torch@@YAXXZ) [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4049: locally defined symbol ?THPDtypeType@@3U_typeobject@@A (struct _typeobject THPDtypeType
) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_variable_methods.obj : warning LNK4049: locally defined symbol ?THPDtypeType@@3U_typeobject@@A (struct _typeobje
ct THPDtypeType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_sugared_value.obj : warning LNK4049: locally defined symbol ?THPDtypeType@@3U_typeobject@@A (struct _typeobject
THPDtypeType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_tensor.obj : warning LNK4049: locally defined symbol ?THPDtypeType@@3U_typeobject@@A (struct _typeobject THPDtyp
eType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_arg_parser.obj : warning LNK4049: locally defined symbol ?THPDtypeType@@3U_typeobject@@A (struct _typeobject THP
DtypeType) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::TypeErro
r::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
Module.cpp.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::TypeEr
ror::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
tensor_apply.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::Type
Error::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
tensor_new.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::TypeEr
ror::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
tensor_numpy.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::Type
Error::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::TypeErro
r::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_variable.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::T
ypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_variable_indexing.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl
 torch::TypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_tensor.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::Typ
eError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_arg_parser.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch:
:TypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
DynamicTypes.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::Type
Error::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
Generator.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::TypeErr
or::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_function.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::T
ypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_legacy_variable.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl t
orch::TypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_variable_methods.obj : warning LNK4217: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl
torch::TypeError::TypeError(char const *,...)) imported in function "struct _object * __cdecl torch::autograd::THPVaria
ble_type(struct _object *,struct _object *,struct _object *)" (?THPVariable_type@autograd@torch@@YAPEAU_object@@PEAU3@0
0@Z) [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_torch_functions.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl t
orch::TypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
python_nn_functions.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torc
h::TypeError::TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
Device.obj : warning LNK4049: locally defined symbol ??0TypeError@torch@@QEAA@PEBDZZ (public: __cdecl torch::TypeError:
:TypeError(char const *,...)) imported [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4217: locally defined symbol ?ToONNX@jit@torch@@YA?AV?$shared_ptr@UGraph@jit@torch@@@std@@AEA
V34@W4OperatorExportTypes@onnx@2@@Z (class std::shared_ptr&lt;struct torch::jit::Graph&gt; __cdecl torch::jit::ToONNX(class s
td::shared_ptr&lt;struct torch::jit::Graph&gt; &amp;,enum torch::onnx::OperatorExportTypes)) imported in function "void __cdecl t
orch::jit::initJITBindings(struct _object *)" (?initJITBindings@jit@torch@@YAXPEAU_object@@@Z) [C:\pytorch\build\caffe2
\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4217: locally defined symbol ?BlockToONNX@jit@torch@@YAXPEAUBlock@12@0W4OperatorExportTypes@o
nnx@2@V?$unordered_map@PEAUValue@jit@torch@@PEAU123@U?$hash@PEAUValue@jit@torch@@@std@@U?$equal_to@PEAUValue@jit@torch@
@@5@V?$allocator@U?$pair@QEAUValue@jit@torch@@PEAU123@@std@@@5@@std@@@Z (void __cdecl torch::jit::BlockToONNX(struct to
rch::jit::Block *,struct torch::jit::Block *,enum torch::onnx::OperatorExportTypes,class std::unordered_map&lt;struct torc
h::jit::Value *,struct torch::jit::Value *,struct std::hash&lt;struct torch::jit::Value *&gt;,struct std::equal_to&lt;struct tor
ch::jit::Value *&gt;,class std::allocator&lt;struct std::pair&lt;struct torch::jit::Value * const,struct torch::jit::Value *&gt; &gt;
&gt;)) imported in function "void __cdecl torch::jit::initJITBindings(struct _object *)" (?initJITBindings@jit@torch@@YAXP
EAU_object@@@Z) [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4217: locally defined symbol ?PrepareDivisionForONNX@jit@torch@@YAXAEBV?$shared_ptr@UGraph@ji
t@torch@@@std@@@Z (void __cdecl torch::jit::PrepareDivisionForONNX(class std::shared_ptr&lt;struct torch::jit::Graph&gt; cons
t &amp;)) imported in function "void __cdecl torch::jit::initJITBindings(struct _object *)" (?initJITBindings@jit@torch@@YA
XPEAU_object@@@Z) [C:\pytorch\build\caffe2\torch\torch_python.vcxproj]
init.cpp.obj : warning LNK4217: locally defined symbol ?getRecursiveScriptMode@script@jit@torch@@YAAEA_NXZ (bool &amp; __cd
ecl torch::jit::script::getRecursiveScriptMode(void)) imported in function "private: static class pybind11::handle __cd
ecl &lt;lambda_699ff1579e5ea22a9941363e0bb1e852&gt;::&lt;lambda_invoker_cdecl&gt;(struct pybind11::detail::function_call &amp;)" (?&lt;lam
bda_invoker_cdecl&gt;@&lt;lambda_699ff1579e5ea22a9941363e0bb1e852&gt;@@CA?AVhandle@pybind11@@AEAUfunction_call@detail@3@@Z) [C:\
pytorch\build\caffe2\torch\torch_python.vcxproj]
  torch_python.vcxproj -&gt; C:\pytorch\build\bin\Release\torch_python.dll
Traceback (most recent call last):
  File "setup.py", line 752, in &lt;module&gt;
    build_deps()
  File "setup.py", line 320, in build_deps
    build_dir='build')
  File "C:\pytorch\tools\build_pytorch_libs.py", line 88, in build_caffe2
    check_call(build_cmd, cwd=build_dir, env=my_env)
  File "C:\Users\gjben\Anaconda3\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--target', 'install', '--config', 'Release', '--', '/maxcpucount:8']' returned non-zero exit status 1.
</code></pre>
<p>It happens when running:</p>
<pre><code class="lang-auto">python setup.py build --cmake
</code></pre>

username: peterjc123
cooked: <p>I couldn’t find any error in your post.</p>

username: Bega
cooked: <p>Here you have it:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "setup.py", line 752, in &lt;module&gt;
    build_deps()
  File "setup.py", line 320, in build_deps
    build_dir='build')
  File "C:\pytorch\tools\build_pytorch_libs.py", line 88, in build_caffe2
    check_call(build_cmd, cwd=build_dir, env=my_env)
  File "C:\Users\gjben\Anaconda3\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--target', 'install', '--config', 'Release', '--', '/maxcpucount:8']' returned non-zero exit status 1.
</code></pre>
<p>And here you have the lines I could record from the full-log:<br>
<a href="https://github.com/GibranBenitez/temporal/blob/master/second_VS" rel="nofollow noopener">partial-log</a></p>

username: Bega
cooked: <p>Btw, this time I used:</p>
<pre><code class="lang-auto">set CMAKE_GENERATOR=Visual Studio 15 2017
</code></pre>
<p>Therefore I couln’t find any file</p>
<aside class="quote no-group" data-username="peterjc123" data-post="4" data-topic="47541">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peterjc123/48/2790_2.png" class="avatar"> peterjc123:</div>
<blockquote>
<p>ninja.build</p>
</blockquote>
</aside>

username: peterjc123
cooked: <p>Would you please use Ninja as the generator? I didn’t find any relevant clues in your log that may cause the build failure. Actually, I’m also facing some really weird issues when using MSBuild and CMake 3.14.</p>

username: Bega
cooked: <p>Sure, I will let you know the results as soon as I success or get something weird.</p>
<p>Once more, thank you for your help.</p>

username: Bega
cooked: <p>Thank you very much for your help.<br>
It worked. In resume:</p>
<ul>
<li>I changed the build directory to ‘C:\pytorch’  (no spaces in it)</li>
<li>I used ninja as the generator following:<br>
<a href="https://discuss.pytorch.org/t/pytorch-build-from-source-on-windows/40288/17">Install with ninja</a>
</li>
</ul>
<p>Just as reference this is my env info:</p>
<pre><code class="lang-auto">PyTorch version: 1.2.0a0+f2623c7
Is debug build: No
CUDA used to build PyTorch: 10.0

OS: Microsoft Windows 10 Home
GCC version: (tdm64-1) 5.1.0
CMake version: version 3.14.5

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 10.0.130
GPU models and configuration: GPU 0: GeForce GTX 1070
Nvidia driver version: 425.25
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin\cudnn64_7.dll

Versions of relevant libraries:
[pip] numpy==1.12.1
[pip] numpydoc==0.6.0
[pip] torch==1.2.0a0+f2623c7
[conda] mkl                       2019.0                    &lt;pip&gt;
[conda] mkl                       2017.0.1                      0
[conda] mkl-include               2019.0                    &lt;pip&gt;
[conda] mkl-service               1.1.2                    py36_3
[conda] torch                     1.2.0a0+f2623c7           &lt;pip&gt;
</code></pre>