Compiling cpp + libtorch code in VS 2017

#1

Hello,

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 libtorch/include to C++ include dirs & libtorch/lib to linker

#define _CRT_SECURE_NO_WARNINGS
#include "torch/script.h"
#include <iostream>
#include <memory>
#include <fstream>


#include <stdint.h>
#include <vector>

typedef unsigned char BYTE;

using namespace std;

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

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

}

std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);
return 0;
}

I get errors like these:

|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;)" (__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&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &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;)" (__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;)" (__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&lt;struct c10::ivalue::Future,struct c10::detail::intrusive_target_default_null_type&lt;struct c10::ivalue::Future&gt; &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; __cdecl c10::IValue::operator=(struct c10::IValue const &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; __cdecl c10::IValue::operator=(struct c10::IValue &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; __cdecl at::Tensor::operator=(class at::Tensor const &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;)" (__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;)" (__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; __cdecl c10::intrusive_ptr_target::operator=(class c10::intrusive_ptr_target const &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; __cdecl c10::intrusive_ptr_target::operator=(class c10::intrusive_ptr_target &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&lt;struct torch::jit::script::Module&gt; __cdecl torch::jit::load(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;,class c10::optional&lt;struct c10::Device&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; __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||

Any help would be greatly appreciated. Thanks in advance! :slight_smile:

In Windows 10, how to install/include LibTorch in Visual Studio Professional 2015?
(Pu Jiachen) #2

You will need to add the library torch.lib in the linking configuration explicitly.

1 Like
#3

Thank you, works perfect!!

(Gibran Benitez) #4

I would like to ask a question relate to this.

If I want to compile with VS2017 and I have installed pytorch from source, do I have to install libtorch too?

I don’t have clear what is the relation between pytorch and libtorch.

(Pu Jiachen) #5

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.

1 Like