How do i to create a new project in C++ to run libtorch without cmake?

username: Mr.Crab
cooked: <p>I have followed the instruction to run the libtorch code for model inference. But now, i want to create a new C++ project to do this which doesn’t realise it through cmake, how should i do that, is there any tutorial?</p>

username: glaringlee
cooked: <p>Please download the zip version of libtorch. Then try this.</p>
<p>install_root = ‘/xxx/yyy/zzz/libtorch’ which is the path or libtorch</p>
<p>g++ example-app.cpp -I${install_root}/include -I${install_root}/include/torch/csrc/api/include -D_GLIBCXX_USE_CXX11_ABI=0 -std=gnu++14 -L${install_root}/lib -Wl,-R${install_root}/lib -ltorch -ltorch_cpu -lc10 -o example-app</p>

username: sharvil
cooked: <p>Does it make sense to write up some pkg-config files to make it easier / more obvious which compiler and linker flags to use?</p>

username: glaringlee
cooked: <p>We use cmake to retrieve correct flags and dependencies that needed, the reason is that pytorch is changing very fast, the lib names, flags, dependencies keep changing. pkg-config is not easy to maintain here.</p>

username: Aviais
cooked: <p>Thanks.<br>
I’m also trying to create a new CPP project to run libtorch without cmake.<br>
I tried to follow you instructions and I’m getting the following errors:</p>
<p><em><strong>COMMANDS</strong></em>:</p>
<ol>
<li>
<p>export install_root=mnt/c/CP/REPOSITORIES/CPU/libtorch-sh<br>
ared-with-deps-latest/libtorch</p>
</li>
<li>
<p>g++ PytorchTest.cpp -I${install_root}/include -I${install_root}/include/torch/csrc/api/include -D_GLIBCXX_USE_CXX11_ABI=0 -std=gnu++14 -L${install_root}/lib -Wl,-R${install_root}/lib -ltorch -ltorch_cpu -lc10 -o PytorchTest</p>
</li>
</ol>
<p><em><strong>ERROR</strong></em><br>
/usr/bin/ld: cannot find -ltorch<br>
/usr/bin/ld: cannot find -ltorch_cpu<br>
/usr/bin/ld: cannot find -lc10<br>
collect2: error: ld returned 1 exit status</p>
<p>I see these libraries inside libtorch/lib.<br>
Can you please help?</p>

username: glaringlee
cooked: <p><a class="mention" href="/u/aviais">@aviais</a>, if it is not a typo, please change your ‘export’ to the following: (you missed a ‘/’ which will make the path not absolute)<br>
export install_root=/mnt/c/CP/REPOSITORIES/CPU/libtorch-shared-with-deps-latest/libtorch</p>
<p>let me know whether it works</p>

username: Aviais
cooked: <p>Thanks,<br>
I’m running this command:<br>
sudo g++ /mnt/c/CP/REPOSITORIES/example-app/build/example-app.cpp -I${install_root}/include -I${install_root}/include/torch/csrc/api/include -D_GLIBCXX_USE_CXX11_ABI=0 -std=gnu++14 -L${install_<br>
root}/lib -Wl,-R${install_root}/lib -ltorch -ltorch_cpu -lc10 -o example-app</p>
<p>Error log:<br>
/tmp/cczXnrIF.o: In function <code>c10::dispatchKeyToBackend(c10::DispatchKey)': example-app.cpp:(.text+0x2f3): undefined reference to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’<br>
/tmp/cczXnrIF.o: In function <code>c10::backendToDeviceType(c10::Backend)': example-app.cpp:(.text+0x519): undefined reference to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’<br>
example-app.cpp:(.text+0x61a): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>c10::typeMetaToScalarType(caffe2::TypeMeta)’:<br>
example-app.cpp:(.text+0xcc2): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>main’:<br>
example-app.cpp:(.text+0x116c): undefined reference to <code>torch::jit::load(std::string const&amp;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::string, std::string, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;&amp;)' /tmp/cczXnrIF.o: In function </code>c10::Device::validate()’:<br>
example-app.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0xe8): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' example-app.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0x20b): undefined reference to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’<br>
/tmp/cczXnrIF.o: In function <code>c10::operator&lt;&lt;(std::ostream&amp;, c10::Layout)': example-app.cpp:(.text._ZN3c10lsERSoNS_6LayoutE[_ZN3c10lsERSoNS_6LayoutE]+0x122): undefined reference to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’<br>
/tmp/cczXnrIF.o: In function <code>c10::intrusive_ptr_target::~intrusive_ptr_target()': example-app.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x105): undefined reference to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’<br>
example-app.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x216): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o:example-app.cpp:(.text._ZNK3c1013TensorOptions18computeDispatchKeyEv[_ZNK3c1013TensorOptions18computeDispatchKeyEv]+0x1df): more undefined references to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’ follow<br>
/tmp/cczXnrIF.o: In function <code>torch::jit::Object::get_method(std::string const&amp;) const': example-app.cpp:(.text._ZNK5torch3jit6Object10get_methodERKSs[_ZNK5torch3jit6Object10get_methodERKSs]+0x39): undefined reference to </code>torch::jit::Object::find_method(std::string const&amp;) const’<br>
example-app.cpp:(.text._ZNK5torch3jit6Object10get_methodERKSs[_ZNK5torch3jit6Object10get_methodERKSs]+0x13f): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>torch::ones(c10::ArrayRef, c10::TensorOptions const&amp;)’:<br>
example-app.cpp:(.text._ZN5torch4onesEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4onesEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x9f): undefined reference to <code>c10::Symbol::fromQualString(std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>torch::jit::Module::forward(std::vector&lt;c10::IValue, std::allocator<a>c10::IValue</a> &gt;)’:<br>
example-app.cpp:(.text._ZN5torch3jit6Module7forwardESt6vectorIN3c106IValueESaIS4_EE[<em>ZN5torch3jit6Module7forwardESt6vectorIN3c106IValueESaIS4_EE]+0xf5): undefined reference to <code>torch::jit::Method::operator()(std::vector&lt;c10::IValue, std::allocator&lt;c10::IValue&gt; &gt;, std::unordered_map&lt;std::string, c10::IValue, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, c10::IValue&gt; &gt; &gt; const&amp;)' /tmp/cczXnrIF.o: In function </code>c10::intrusive_ptr&lt;c10::TensorImpl, c10::UndefinedTensorImpl&gt;::retain</em>()’:<br>
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_10TensorImplENS_19UndefinedTensorImplEE7retain_Ev[<em>ZN3c1013intrusive_ptrINS_10TensorImplENS_19UndefinedTensorImplEE7retain_Ev]+0xfb): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>c10::intrusive_ptr&lt;c10::ivalue::Object, c10::detail::intrusive_target_default_null_type<a>c10::ivalue::Object</a> &gt;::retain</em>()’:<br>
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_6ivalue6ObjectENS_6detail34intrusive_target_default_null_typeIS2_EEE7retain_Ev[<em>ZN3c1013intrusive_ptrINS_6ivalue6ObjectENS_6detail34intrusive_target_default_null_typeIS2_EEE7retain_Ev]+0xfb): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>c10::intrusive_ptr&lt;c10::detail::ListImpl, c10::detail::intrusive_target_default_null_type<a>c10::detail::ListImpl</a> &gt;::retain</em>()’:<br>
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_6detail8ListImplENS1_34intrusive_target_default_null_typeIS2_EEE7retain_Ev[_ZN3c1013intrusive_ptrINS_6detail8ListImplENS1_34intrusive_target_default_null_typeIS2_EEE7retain_Ev]+0xfb): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>at::Tensor c10::KernelFunction::callUnboxed&lt;at::Tensor, at::Tensor const&amp;, long, long, long, long&gt;(c10::OperatorHandle const&amp;, at::Tensor const&amp;, long, long, long, long) const’:<br>
example-app.cpp:(.text.<em>ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJRKS3_llllEEET_RKNS_14OperatorHandleEDpT0</em>[<em>ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJRKS3_llllEEET_RKNS_14OperatorHandleEDpT0</em>]+0x1a1): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o: In function </code>at::Tensor c10::KernelFunction::callUnboxed&lt;at::Tensor, c10::ArrayRef, c10::TensorOptions const&amp;&gt;(c10::OperatorHandle const&amp;, c10::ArrayRef, c10::TensorOptions const&amp;) const’:<br>
example-app.cpp:(.text.<em>ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJNS_8ArrayRefIlEERKNS_13TensorOptionsEEEET_RKNS_14OperatorHandleEDpT0</em>[<em>ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJNS_8ArrayRefIlEERKNS_13TensorOptionsEEEET_RKNS_14OperatorHandleEDpT0</em>]+0x15c): undefined reference to <code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)' /tmp/cczXnrIF.o:example-app.cpp:(.text._ZN3c104impl19boxAndCallBoxedFuncIN2at6TensorEJRKS3_llllEEENSt9enable_ifIXaasrNS_4guts8negationINS7_11disjunctionIJSt19is_lvalue_referenceIT_ENS8_INS9_IJSt16is_constructibleINS_6IValueEJSB_EESt7is_sameINS_13TensorOptionsESB_ESG_IvSB_EEEEEESG_INS_8ArrayRefIlEESB_EDpNS8_INS9_IJSD_ISE_JNSt5decayIT0_E4typeEEESG_ISH_SS_ESG_IvSS_EEEEEEEEEEE5valuentsrSJ_5valueESB_E4typeEPFvPNS_14OperatorKernelERKNS_14OperatorHandleEPSt6vectorISE_SaISE_EEES14_S17_DpSQ_[_ZN3c104impl19boxAndCallBoxedFuncIN2at6TensorEJRKS3_llllEEENSt9enable_ifIXaasrNS_4guts8negationINS7_11disjunctionIJSt19is_lvalue_referenceIT_ENS8_INS9_IJSt16is_constructibleINS_6IValueEJSB_EESt7is_sameINS_13TensorOptionsESB_ESG_IvSB_EEEEEESG_INS_8ArrayRefIlEESB_EDpNS8_INS9_IJSD_ISE_JNSt5decayIT0_E4typeEEESG_ISH_SS_ESG_IvSS_EEEEEEEEEEE5valuentsrSJ_5valueESB_E4typeEPFvPNS_14OperatorKernelERKNS_14OperatorHandleEPSt6vectorISE_SaISE_EEES14_S17**_DpSQ_]+0x197): more undefined references to </code>c10::Error::Error(c10::SourceLocation, std::string const&amp;)’ follow**<br>
<strong>collect2: error: ld returned 1 exit status</strong></p>
<p>What is the problem?</p>

username: ws123
cooked: <p>Thanks <a class="mention" href="/u/glaringlee">@glaringlee</a>, for libtorch-cxx11-abi-shared-with-deps-1.7.1.zip, above command is working by setting D_GLIBCXX_USE_CXX11_ABI=1</p>