My First C++ Pytorch Program | Link Error

I am using codeblocks.

#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";

I have add in linker path.

Getting the following output
main.cpp:(.text.startup+0x50): undefined reference to `torch::jit::load(std::string const&, c10::optional<c10::Device>, std::unordered_map<std::string, std::string, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::string> > >&)'
collect2: error: ld returned 1 exit status

I think torch/torch.h is the canonical include for libtorch.
You didn’t show your command line, do you have -ltorch in it? You might also need caffe2 and/or c10 depending on the version you are on.

Best regards


Try setting this flag "-D_GLIBCXX_USE_CXX11_ABI=0" with CMake.

If you are passing a relative path to the libtorch in CMake, make sure you have the $PWD in the path:

cmake -DCMAKE_PREFIX_PATH=$PWD/../relative/path/to/libtorch ..