Can't use iOS libtorch as a dependency for a library using Cocoapods

I’m using libtorch as a dependency for our library using Cocoapods. It compiles fine when I use I use libtorch in an app, but fails when I import libtorch headers

Specific Steps:

  1. pod lib create <pod>
  2. Add the C++ library as a dependency on the podspec s.dependency 'LibTorch', '~> 1.3.0'
  3. Add Objective-C files TestClass.h and TestClass.m
  4. Import the library on TestClass.m
  
#import "TestClass.h"
#import <LibTorch/LibTorch.h>

@implementation TestClass



@end
  1. pod install
  2. Build the project, then it fails to build.

Error when buidling: 'torch/script.h' not found
In Pods/LibTorch/Core/Libtorch.h

#ifndef LibTorch_h
#define LibTorch_h

#include <torch/script.h>

#endif

I’ve filed a similar issue in cocoapods here https://github.com/CocoaPods/CocoaPods/issues/9678 but I think this may be a libtorch issue with how its header/include paths are set.

Here is a sample project: https://github.com/mjjimenez/SamplePod

@xta0 can I ask you for some help on this?

@xtao I’ve filed an issue here if you need something you can track: https://github.com/pytorch/pytorch/issues/36387

@mark_jimenez if you want to wrap pytorch into your custom podspec, you need to specify the header search path manually. see here for example - https://github.com/xta0/PytorchExpObjC/blob/master/PytorchExpObjc.podspec

@xta0 it works perfectly. Thank you again for your help!

Not sure why is it in cocoapods you still need to redeclare header paths in your own podspec, if it’s already declared in libtorch’s podspec. But hey, as long as it works.

I’ll be closing the issue in github.

1 Like

Hello @xta0,

With your podspec as exemple, I successfully added libtorch as a dependency of a private pod. Thank you for that !

Also I have a linker issue if I try to run a test target. I have the same issue, in the app using the private pod.

I tried with different version of libtorch, but I have the error with all of them (1.6.1, 1.7 and 1.7.1).

The error is :
Undefined symbols for architecture x86_64:

  "_clog_vlog_error", referenced from:
      pytorch_qnnp_log_error(char const*, ...) in libtorch_cpu.a(qconv_prepack.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Same error with arm64 if I try to run the tests on my device.

My podspec look like that :

  ...

  s.ios.deployment_target = '13.0'

  s.frameworks = [
  "Accelerate",
  "AssetsLibrary",
  "AVFoundation",
  "CoreGraphics",
  "CoreImage",
  "CoreMedia",
  "CoreVideo",
  "Foundation",
  "QuartzCore",
  "UIKit",
  "Vision"
  ]
  
  s.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
  s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
  
  s.source_files = 'PRIVATEPOD/Classes/**/*'
  s.resource_bundles = {
    'Json' => ['PRIVATEPOD/Resources/**/*{.json}'],
    'Model' => ['PRIVATEPOD/Resources/**/*{.pt}']
  }
  s.public_header_files = ["PRIVATEPOD/Classes/**/*{.h,.hpp}"]
  
  s.static_framework = true
  s.dependency 'OpenCV2', '4.2.0'
  s.dependency 'LibTorch'
  s.dependency 'ZIPFoundation', '~> 0.9'
  
  s.pod_target_xcconfig = {
      'HEADER_SEARCH_PATHS' => '$(inherited) "${PODS_ROOT}/LibTorch/install/include"'
  }
  s.library = 'c++', 'stdc++'
end

I tried many many things, but I’m stuck, and I cannot run my tests anymore :-/.

@xta0 do you have an idea to fix that ?

Thank you

I created a issue on the github repo, with a sample project :