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

mark_jimenez: <p>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</p>
<p>Specific Steps:</p>
<ol>
<li><code>pod lib create &lt;pod&gt;</code></li>
<li>Add the C++ library as a dependency on the podspec  <code>s.dependency 'LibTorch', '~&gt; 1.3.0'</code>
</li>
<li>Add Objective-C files  <code>TestClass.h</code>  and  <code>TestClass.m</code>
</li>
<li>Import the library on <code>TestClass.m</code>
</li>
</ol>
<pre><code class="lang-auto">  
#import "TestClass.h"
#import &lt;LibTorch/LibTorch.h&gt;

@implementation TestClass



@end
</code></pre>
<ol start="4">
<li><code>pod install</code></li>
<li>Build the project, then it fails to build.</li>
</ol>
<p>Error when buidling:  <code>'torch/script.h' not found</code><br>
In  <code>Pods/LibTorch/Core/Libtorch.h</code></p>
<pre><code class="lang-auto">#ifndef LibTorch_h
#define LibTorch_h

#include &lt;torch/script.h&gt;

#endif
</code></pre>
<p>I’ve filed a similar issue in cocoapods here <a href="https://github.com/CocoaPods/CocoaPods/issues/9678" rel="nofollow noopener">https://github.com/CocoaPods/CocoaPods/issues/9678</a> but I think this may be a libtorch issue with how its header/include paths are set.</p>
<p>Here is a sample project: <a href="https://github.com/mjjimenez/SamplePod" rel="nofollow noopener">https://github.com/mjjimenez/SamplePod</a></p>
<p><a class="mention" href="/u/xta0">@xta0</a> can I ask you for some help on this?</p>

mark_jimenez: <p><span class="mention">@xtao</span> I’ve filed an issue here if you need something you can track: <a href="https://github.com/pytorch/pytorch/issues/36387" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/36387</a></p>

xta0: <p><a class="mention" href="/u/mark_jimenez">@mark_jimenez</a> if you want to wrap pytorch into your custom podspec, you need to specify the header search path manually. see here for example - <a href="https://github.com/xta0/PytorchExpObjC/blob/master/PytorchExpObjc.podspec" rel="nofollow noopener">https://github.com/xta0/PytorchExpObjC/blob/master/PytorchExpObjc.podspec</a></p>

mark_jimenez: <p><a class="mention" href="/u/xta0">@xta0</a> it works perfectly. Thank you again for your help!</p>
<p>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.</p>
<p>I’ll be closing the issue in github.</p>

hschouman: <p>Hello <a class="mention" href="/u/xta0">@xta0</a>,</p>
<p>With your podspec as exemple, I successfully added libtorch as a dependency of a private pod. Thank you for that !</p>
<p>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.</p>
<p>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).</p>
<p>The error is :<br>
Undefined symbols for architecture x86_64:</p>
<pre><code class="lang-auto">  "_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)
</code></pre>
<p>Same error with arm64 if I try to run the tests on my device.</p>
<p>My podspec look like that :</p>
<pre><code class="lang-auto">  ...

  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*]' =&gt; 'arm64' }
  s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' =&gt; 'arm64' }
  
  s.source_files = 'PRIVATEPOD/Classes/**/*'
  s.resource_bundles = {
    'Json' =&gt; ['PRIVATEPOD/Resources/**/*{.json}'],
    'Model' =&gt; ['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', '~&gt; 0.9'
  
  s.pod_target_xcconfig = {
      'HEADER_SEARCH_PATHS' =&gt; '$(inherited) "${PODS_ROOT}/LibTorch/install/include"'
  }
  s.library = 'c++', 'stdc++'
end
</code></pre>
<p>I tried many many things, but I’m stuck, and I cannot run my tests anymore :-/.</p>
<p><a class="mention" href="/u/xta0">@xta0</a> do you have an idea to fix that ?</p>
<p>Thank you</p>

hschouman: <p>I created a issue on the github repo, with a sample project :<br>
</p><aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/49456" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/49456" target="_blank" rel="noopener nofollow ugc">Impossible to run tests target with LibTorch as dependency inside a cocoapods.</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-12-16" data-time="10:15:05" data-timezone="UTC">10:15AM - 16 Dec 20 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/hschouman" target="_blank" rel="noopener nofollow ugc">
          <img alt="hschouman" src="https://avatars3.githubusercontent.com/u/8625118?v=4" class="onebox-avatar-inline" width="20" height="20">
          hschouman
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">🐛 Bug
Impossible to run tests target with LibTorch as dependency inside a cocoapods.
To Reproduce
Steps to reproduce the behavior:
Download or clone my...</p>
</div>

<div class="labels">
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">module: ios</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">triaged</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>