[SOLVED] Linker error when loading a pre-trained model with torch script and compiling with g++

lui.bonati: <p>Hi all,<br>
I am trying to add libtorch library to a c++ project, and I need to compile it with g++. Following this topic <a href="https://discuss.pytorch.org/t/solved-build-the-c-frontend-using-g-or-any-other-compiler-no-cmake/35156">[SOLVED] Build the C++ frontend using g++ or any other compiler (no cmake)</a> I have been able to succesfully compile it, and to train a model inside my project.</p>
<p>Now I would like to load a pre-trained model with pytorch, using TorchScript. Let’s take as minimal example the code provided in the tutorial <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">LOADING A PYTORCH MODEL IN C++</a>:</p>
<pre><code class="lang-auto">#include &lt;torch/script.h&gt; // One-stop header.
#include &lt;iostream&gt;
#include &lt;memory&gt;

int main(int argc, const char* argv[]) {
  if (argc != 2) {
    std::cerr &lt;&lt; "usage: example-app &lt;path-to-exported-script-module&gt;\n";
    return -1;
  }
  // Deserialize the ScriptModule from a file using torch::jit::load().
     std::shared_ptr&lt;torch::jit::script::Module&gt; module = torch::jit::load(argv[1]);

     assert(module != nullptr);
     std::cout &lt;&lt; "ok\n";
}
</code></pre>
<p>If I try to compile it with g++ I get undefined reference for the jit::load function:</p>
<pre><code class="lang-auto">g++ -I ${LIBTORCH}/include/torch/csrc/api/include/ -I ${LIBTORCH}/include/ -I ${LIBTORCH}/include/torch/ -L ${LIBTORCH}/lib -ltorch -lcaffe2 -lc10 -D_GLIBCXX_USE_CXX11_ABI=1 example-app.cpp -o example-app 

example-app.cpp:(.text+0x38e): undefined reference to `torch::jit::load(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;)'
/tmp/ccD9MHCS.o: In function `torch::jit::SourceRange::highlight(std::ostream&amp;) const':
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x196): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x288): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x3c1): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x50d): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
collect2: error: ld returned 1 exit status
</code></pre>
<p>It seems like is not able to retrieve the jit functions from the linked libraries. Do you have any suggestion about which library and/or include file is missing?</p>
<p>Any help will be appreciated!</p>
<p>Best,<br>
Luigi</p>

tom: <p>My first two things to check would be:</p>
<ul>
<li>You are using the libraries (.so) and headers from the exact same PyTorch distribution?</li>
<li>Do you have C++ -ABI compatibility between the compiler used to compile PyTorch and yours? (i.e. try <code>-D_GLIBCXX_USE_CXX11_ABI=0</code> as in the linked post)</li>
</ul>
<p>Note that it appears to find the <code>torch::jit::load</code> all right, but that in turn needs C10 functions that it doesn’t find.</p>
<p>Best regards</p>
<p>Thomas</p>

lui.bonati: <p>You are right, the C++ -ABI flag was the issue, turning it off as suggested solves the problem.</p>
<p>Thank you,<br>
Best regards</p>
<p>Luigi</p>

yf225: <p>We now have new ABI binaries for libtorch. They can be found on<br>
<a href="http://pytorch.org/" rel="nofollow noopener">http://pytorch.org</a>, or at <a href="https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441</a>, which would also resolve this problem.</p>

lui.bonati: <p>I tried to use the new ABI binaries as suggested by <a class="mention" href="/u/yf225">@yf225</a>.</p>
<p>I downloaded the 1.4.0+cpu stable version but it does not work, even with the <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">minimal example</a>.</p>
<pre><code class="lang-auto">g++ -I ${LIBTORCH}/include/torch/csrc/api/include/ -I ${LIBTORCH}/include/ -I ${LIBTORCH}/include/torch/ -L ${LIBTORCH}/lib -ltorch -lc10 example-app.cpp -o example-app 
</code></pre>
<pre><code class="lang-auto">/tmp/ccNumZyv.o: In function `main':
example-app.cpp:(.text+0x14d): undefined reference to `torch::jit::load(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;)'
/tmp/ccNumZyv.o: In function `c10::intrusive_ptr_target::~intrusive_ptr_target()':
example-app.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x12b): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
example-app.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x259): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/tmp/ccNumZyv.o: In function `c10::intrusive_ptr&lt;c10::intrusive_ptr_target, c10::detail::intrusive_target_default_null_type&lt;c10::intrusive_ptr_target&gt; &gt;::reclaim(c10::intrusive_ptr_target*)':
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_20intrusive_ptr_targetENS_6detail34intrusive_target_default_null_typeIS1_EEE7reclaimEPS1_[_ZN3c1013intrusive_ptrINS_20intrusive_ptr_targetENS_6detail34intrusive_target_default_null_typeIS1_EEE7reclaimEPS1_]+0x145): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/tmp/ccNumZyv.o: In function `c10::intrusive_ptr&lt;c10::ivalue::Object, c10::detail::intrusive_target_default_null_type&lt;c10::ivalue::Object&gt; &gt;::retain_()':
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_6ivalue6ObjectENS_6detail34intrusive_target_default_null_typeIS2_EEE7retain_Ev[_ZN3c1013intrusive_ptrINS_6ivalue6ObjectENS_6detail34intrusive_target_default_null_typeIS2_EEE7retain_Ev]+0x118): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
collect2: error: ld returned 1 exit status
</code></pre>
<p>If I add -D_GLIBCXX_USE_CXX11_ABI=1 it throws the same error. The version of the compiler is the following:</p>
<pre><code class="lang-auto">g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
</code></pre>
<p>Any suggestions?<br>
Thank you</p>
<p>Luigi</p>

deeperlearner: <p>Hi. I have the same problem. Do you solve it? Thanks.</p>