Compiling cpp + libtorch code in VS 2017

username: ash_gamma
cooked: <p>Hello,</p>
<p>Here’s my simple cpp code that just loads the model. I’m trying to compile this code in Windows 7, VS 2017. I’ve included path to <code>libtorch/include</code> to C++ include dirs &amp; <code>libtorch/lib</code> to linker</p>
<pre><code class="lang-auto">#define _CRT_SECURE_NO_WARNINGS
#include "torch/script.h"
#include &lt;iostream&gt;
#include &lt;memory&gt;
#include &lt;fstream&gt;


#include &lt;stdint.h&gt;
#include &lt;vector&gt;

typedef unsigned char BYTE;

using namespace std;

int main(int argc, const char* argv[]) {

if (argc != 3) {
std::cerr &lt;&lt; "usage: example-app &amp;lt;path-to-exported-script-module&amp;gt; &amp;lt;path-to-bin-file&amp;gt;\n";
return -1;

}

std::shared_ptr&lt;torch::jit::script::Module&gt; module = torch::jit::load(argv[1]);
return 0;
}
</code></pre>
<p>I get errors like these:</p>
<pre><code class="lang-auto">|Severity|Code|Description|Project|File|Line|Suppression State|
| --- | --- | --- | --- | --- | --- | --- |
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl c10::IValue::~IValue(void)" (__imp_??1IValue@c10@@QEAA@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl c10::IValue::IValue(void)" (__imp_??0IValue@c10@@QEAA@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl c10::IValue::IValue(struct c10::IValue const &amp;amp;)" (__imp_??0IValue@c10@@QEAA@AEBU01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl c10::Error::Error(struct c10::SourceLocation,class std::basic_string&amp;lt;char,struct std::char_traits&amp;lt;char&amp;gt;,class std::allocator&amp;lt;char&amp;gt; &amp;gt; const &amp;amp;)" (__imp_??0Error@c10@@QEAA@USourceLocation@1@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl c10::Error::Error(class c10::Error const &amp;amp;)" (__imp_??0Error@c10@@QEAA@AEBV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl at::Tensor::~Tensor(void)" (__imp_??1Tensor@at@@QEAA@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: __cdecl at::Tensor::Tensor(class at::Tensor const &amp;amp;)" (__imp_??0Tensor@at@@QEAA@AEBV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: void __cdecl c10::ThreadPool::workOnTasksUntilCompleted(class c10::intrusive_ptr&amp;lt;struct c10::ivalue::Future,struct c10::detail::intrusive_target_default_null_type&amp;lt;struct c10::ivalue::Future&amp;gt; &amp;gt;)" (__imp_?workOnTasksUntilCompleted@ThreadPool@c10@@QEAAXV?$intrusive_ptr@UFuture@ivalue@c10@@U?$intrusive_target_default_null_type@UFuture@ivalue@c10@@@detail@3@@2@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: virtual __cdecl c10::Error::~Error(void)" (__imp_??1Error@c10@@UEAA@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: struct c10::IValue &amp;amp; __cdecl c10::IValue::operator=(struct c10::IValue const &amp;amp;)&amp;amp; " (__imp_??4IValue@c10@@QEGAAAEAU01@AEBU01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: struct c10::IValue &amp;amp; __cdecl c10::IValue::operator=(struct c10::IValue &amp;amp;&amp;amp;)&amp;amp; " (__imp_??4IValue@c10@@QEGAAAEAU01@$$QEAU01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) public: class at::Tensor &amp;amp; __cdecl at::Tensor::operator=(class at::Tensor const &amp;amp;)&amp;amp; " (__imp_??4Tensor@at@@QEGAAAEAV01@AEBV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) protected: __cdecl c10::intrusive_ptr_target::intrusive_ptr_target(void)" (__imp_??0intrusive_ptr_target@c10@@IEAA@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) protected: __cdecl c10::intrusive_ptr_target::intrusive_ptr_target(class c10::intrusive_ptr_target const &amp;amp;)" (__imp_??0intrusive_ptr_target@c10@@IEAA@AEBV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) protected: __cdecl c10::intrusive_ptr_target::intrusive_ptr_target(class c10::intrusive_ptr_target &amp;amp;&amp;amp;)" (__imp_??0intrusive_ptr_target@c10@@IEAA@$$QEAV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) protected: virtual __cdecl c10::intrusive_ptr_target::~intrusive_ptr_target(void)" (__imp_??1intrusive_ptr_target@c10@@MEAA@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) protected: class c10::intrusive_ptr_target &amp;amp; __cdecl c10::intrusive_ptr_target::operator=(class c10::intrusive_ptr_target const &amp;amp;)" (__imp_??4intrusive_ptr_target@c10@@IEAAAEAV01@AEBV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) protected: class c10::intrusive_ptr_target &amp;amp; __cdecl c10::intrusive_ptr_target::operator=(class c10::intrusive_ptr_target &amp;amp;&amp;amp;)" (__imp_??4intrusive_ptr_target@c10@@IEAAAEAV01@$$QEAV01@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) private: __cdecl caffe2::TypeIdentifier::TypeIdentifier(unsigned short)" (__imp_??0TypeIdentifier@caffe2@@AEAA@G@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) private: virtual void __cdecl c10::intrusive_ptr_target::release_resources(void)" (__imp_?release_resources@intrusive_ptr_target@c10@@EEAAXXZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) class std::shared_ptr&amp;lt;struct torch::jit::script::Module&amp;gt; __cdecl torch::jit::load(class std::basic_string&amp;lt;char,struct std::char_traits&amp;lt;char&amp;gt;,class std::allocator&amp;lt;char&amp;gt; &amp;gt; const &amp;amp;,class c10::optional&amp;lt;struct c10::Device&amp;gt;)" (__imp_?load@jit@torch@@YA?AV?$shared_ptr@UModule@script@jit@torch@@@std@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@V?$optional@UDevice@c10@@@c10@@@Z)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK2001|unresolved external symbol "__declspec(dllimport) class c10::ThreadPool &amp;amp; __cdecl c10::global_work_queue(void)" (__imp_?global_work_queue@c10@@YAAEAVThreadPool@1@XZ)|pytorch_cpp_test|D:\User\pytorch_cpp_test\pytorch_cpp_test\pytorch_cpp_test.obj|1||
|Error|LNK1120|22 unresolved externals|pytorch_cpp_test|D:\User\pytorch_cpp_test\x64\Release\pytorch_cpp_test.exe|1||
</code></pre>
<p>Any help would be greatly appreciated. Thanks in advance! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: peterjc123
cooked: <p>You will need to add the library <code>torch.lib</code> in the linking configuration explicitly.</p>

username: ash_gamma
cooked: <p>Thank you, works perfect!!</p>

username: Bega
cooked: <p>I would like to ask a question relate to this.</p>
<p>If I want to compile with VS2017 and I have installed pytorch from source, do I have to install libtorch too?</p>
<p>I don’t have clear what is the relation between pytorch and libtorch.</p>

username: peterjc123
cooked: <p>No, you don’t need to because it is already contained in the python version. Remember, PyTorch relies on  the C++ part, that is, LibTorch.</p>