Hi all,
I am trying to add libtorch library to a c++ project, and I need to compile it with g++. Following this topic [SOLVED] Build the C++ frontend using g++ or any other compiler (no cmake) I have been able to succesfully compile it, and to train a model inside my project.
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 LOADING A PYTORCH MODEL IN C++:
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
return -1;
}
// Deserialize the ScriptModule from a file using torch::jit::load().
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);
assert(module != nullptr);
std::cout << "ok\n";
}
If I try to compile it with g++ I get undefined reference for the jit::load function:
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<char, std::char_traits<char>, std::allocator<char> > const&, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)'
/tmp/ccD9MHCS.o: In function `torch::jit::SourceRange::highlight(std::ostream&) const':
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x196): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x288): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x3c1): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
example-app.cpp:(.text._ZNK5torch3jit11SourceRange9highlightERSo[_ZNK5torch3jit11SourceRange9highlightERSo]+0x50d): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
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?
Any help will be appreciated!
Best,
Luigi