How to make C++ programs portable?

wayne391: <p>I’ve followed the tutorials and successfully created an example program. While building the program, I encountered several common bugs like missing MKL-DNN libraries and fixed them by building it from the source. Finally, everything works perfectly on my computer <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>However, when I want to migrate it to a clean computer, the program crashed. The reported error was:</p>
<pre><code class="lang-auto">Dyld Error Message:
  Library not loaded: @rpath/libc10.dylib
</code></pre>
<p>I found it’s because of missing dynamic libraries in libtorch, but I cannot assume everyone has it.<br>
I think the possible solutions are:</p>
<ol>
<li>compile the program by static linking.</li>
<li>include the .dll/.dylib with the program</li>
</ol>
<p>I am not familiar with C++.  Are the proposed solutions correct? If so, how to achieve that, or what flags and path should I set in the CMAKE?  I want my program being portable and no need to install extra dependencies on new computers.</p>
<p>Thanks!</p>

KFrank: <p>Hi Wen-Yi!</p>
<aside class="quote no-group" data-username="wayne391" data-post="1" data-topic="68401" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wayne391/48/20171_2.png" class="avatar"> wayne391:</div>
<blockquote>
<p>I found it’s because of missing dynamic libraries in libtorch, but I cannot assume everyone has it.<br>
I think the possible solutions are:</p>
<ol>
<li>compile the program by static linking.</li>
<li>include the .dll/.dylib with the program</li>
</ol>
<p>I am not familiar with C++.  Are the proposed solutions correct?</p>
</blockquote>
</aside>
<p>[Disclaimer:  I have neither built torch nor pytorch.]</p>
<p>Yes, these are the two methods you can use to make your program<br>
portable.  Note, you can use a mixture of the two methods – statically<br>
link to some libraries, and provide dynamic libraries for other libraries.</p>
<aside class="quote no-group">
<blockquote>
<p>If so, how to achieve that, or what flags and path should I set in the CMAKE?</p>
</blockquote>
</aside>
<p>I don’t know.  I’ve never used CMAKE.</p>
<aside class="quote no-group">
<blockquote>
<p>I want my program being portable and no need to install extra dependencies on new computers.</p>
</blockquote>
</aside>
<p>Please note that for portability, you will have to build your program<br>
separately for each operating system you want to run on, and<br>
possibly for different hardware platforms and different versions of<br>
the same operating system.</p>
<p>You would normally do this by building on each of the platforms you<br>
want to run on.  (You can also “cross-compile” and “cross-build,” but<br>
that is an art in itself.)</p>
<p>If you want your program to be casually portable, you can build on<br>
your target systems, and hope for the best.  If you want it to be<br>
commercial-grade portable, you must test, test, test on various<br>
combinations of operating system, hardware, and version and<br>
configuration of operating system.  For commercial-grade portability<br>
you will probably also want to distribute your program as installers<br>
and package-manager packages appropriate for your target systems.</p>
<p>Good luck!</p>
<p>K. Frank</p>

wayne391: <p>Thanks for the reply.</p>
<p>My current plan is to write an MNIST classifier with GUI as a prototype and try to make it become commercial-grade portable, if possible. I think it would be the best template to try the things you mentioned.</p>
<p>Is this practical by merely using libtorch? Or should I try ONNX or something else? Is pytorch/libtorch suitable for this scenario or should I try other frameworks?</p>
<p>Actually, I have no idea how the industrials deploy deep learning models. But because I can write my own CMakeLists.txt, it’s very simple and convenient to compile with other C++ frameworks (e.g., JUCE, QT, …). And this gives me a strong motivation to try libtorch.</p>

forwardmeasure: <p>Hello Wen-Yi,<br>
In addition to the wonderful advice provided by <a class="mention" href="/u/kfrank">@KFrank</a>, I’ll add the following:</p>
<p>You will find that people deploy ML models in a variety of ways, but an approach preferred by many is to train models in Python and run inference in C++. Libtorch has a really nice C++ API and you can write code to construct and train models in C++, as well as to run inference. In my environment, I use C++ for training as well as for inference.</p>
<p>If you do take the C++ route, I would encourage you to compile Libtorch from source and ensure that your LD_LIBRARY_PATH/DYLD_LIBRARY_PATH is set to point to the correct location of your Libtorch libraries. Some folks prefer statically linking, but I always link dynamically, taking care to ensure that my LD_LIBRARY_PATH is correctly set. One thing that CMake will need is to know how to find your Libtorch libraries, and the CMakelists.txt file in the tutorial (<a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html" rel="nofollow noopener">https://pytorch.org/tutorials/advanced/cpp_frontend.html</a>) shows how to accomplish that.</p>
<p>Once you get the base build going, you can move onto building with CUDA/ROCM if you want acceleration.</p>
<p>The C++ tutorial is nicely written and should provide you all you need to get going. In the event you really do embrace the C++ route, I would suggest that you took a look at Conan (<a href="http://conan.io" rel="nofollow noopener">http://conan.io</a>), which is a popular technique to build, package, and deploy C++ libraries. You can look at my github repo (<a href="http://github.com/forwardmeasure" rel="nofollow noopener">github.com/forwardmeasure</a>) for the Conan recipe for Libtorch.</p>
<p>HTH,<br>
PN</p>