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

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?

Please download the zip version of libtorch. Then try this.

install_root = ‘/xxx/yyy/zzz/libtorch’ which is the path or libtorch

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

Does it make sense to write up some pkg-config files to make it easier / more obvious which compiler and linker flags to use?

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.

Thanks.
I’m also trying to create a new CPP project to run libtorch without cmake.
I tried to follow you instructions and I’m getting the following errors:

COMMANDS:

  1. export install_root=mnt/c/CP/REPOSITORIES/CPU/libtorch-sh
    ared-with-deps-latest/libtorch

  2. 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

ERROR
/usr/bin/ld: cannot find -ltorch
/usr/bin/ld: cannot find -ltorch_cpu
/usr/bin/ld: cannot find -lc10
collect2: error: ld returned 1 exit status

I see these libraries inside libtorch/lib.
Can you please help?

@aviais, if it is not a typo, please change your ‘export’ to the following: (you missed a ‘/’ which will make the path not absolute)
export install_root=/mnt/c/CP/REPOSITORIES/CPU/libtorch-shared-with-deps-latest/libtorch

let me know whether it works

Thanks,
I’m running this command:
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_
root}/lib -Wl,-R${install_root}/lib -ltorch -ltorch_cpu -lc10 -o example-app

Error log:
/tmp/cczXnrIF.o: In function c10::dispatchKeyToBackend(c10::DispatchKey)': example-app.cpp:(.text+0x2f3): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)’
/tmp/cczXnrIF.o: In function c10::backendToDeviceType(c10::Backend)': example-app.cpp:(.text+0x519): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)’
example-app.cpp:(.text+0x61a): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function c10::typeMetaToScalarType(caffe2::TypeMeta)’:
example-app.cpp:(.text+0xcc2): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function main’:
example-app.cpp:(.text+0x116c): 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> > >&)' /tmp/cczXnrIF.o: In function c10::Device::validate()’:
example-app.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0xe8): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' example-app.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0x20b): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)’
/tmp/cczXnrIF.o: In function c10::operator<<(std::ostream&, c10::Layout)': example-app.cpp:(.text._ZN3c10lsERSoNS_6LayoutE[_ZN3c10lsERSoNS_6LayoutE]+0x122): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)’
/tmp/cczXnrIF.o: In function c10::intrusive_ptr_target::~intrusive_ptr_target()': example-app.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x105): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)’
example-app.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x216): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o:example-app.cpp:(.text._ZNK3c1013TensorOptions18computeDispatchKeyEv[_ZNK3c1013TensorOptions18computeDispatchKeyEv]+0x1df): more undefined references to c10::Error::Error(c10::SourceLocation, std::string const&)’ follow
/tmp/cczXnrIF.o: In function torch::jit::Object::get_method(std::string const&) const': example-app.cpp:(.text._ZNK5torch3jit6Object10get_methodERKSs[_ZNK5torch3jit6Object10get_methodERKSs]+0x39): undefined reference to torch::jit::Object::find_method(std::string const&) const’
example-app.cpp:(.text._ZNK5torch3jit6Object10get_methodERKSs[_ZNK5torch3jit6Object10get_methodERKSs]+0x13f): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function torch::ones(c10::ArrayRef, c10::TensorOptions const&)’:
example-app.cpp:(.text._ZN5torch4onesEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4onesEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x9f): undefined reference to c10::Symbol::fromQualString(std::string const&)' /tmp/cczXnrIF.o: In function torch::jit::Module::forward(std::vector<c10::IValue, std::allocatorc10::IValue >)’:
example-app.cpp:(.text._ZN5torch3jit6Module7forwardESt6vectorIN3c106IValueESaIS4_EE[ZN5torch3jit6Module7forwardESt6vectorIN3c106IValueESaIS4_EE]+0xf5): undefined reference to torch::jit::Method::operator()(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::string, c10::IValue, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, c10::IValue> > > const&)' /tmp/cczXnrIF.o: In function c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::retain()’:
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_10TensorImplENS_19UndefinedTensorImplEE7retain_Ev[ZN3c1013intrusive_ptrINS_10TensorImplENS_19UndefinedTensorImplEE7retain_Ev]+0xfb): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_typec10::ivalue::Object >::retain()’:
example-app.cpp:(.text._ZN3c1013intrusive_ptrINS_6ivalue6ObjectENS_6detail34intrusive_target_default_null_typeIS2_EEE7retain_Ev[ZN3c1013intrusive_ptrINS_6ivalue6ObjectENS_6detail34intrusive_target_default_null_typeIS2_EEE7retain_Ev]+0xfb): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function c10::intrusive_ptr<c10::detail::ListImpl, c10::detail::intrusive_target_default_null_typec10::detail::ListImpl >::retain()’:
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 c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function at::Tensor c10::KernelFunction::callUnboxed<at::Tensor, at::Tensor const&, long, long, long, long>(c10::OperatorHandle const&, at::Tensor const&, long, long, long, long) const’:
example-app.cpp:(.text.ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJRKS3_llllEEET_RKNS_14OperatorHandleEDpT0[ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJRKS3_llllEEET_RKNS_14OperatorHandleEDpT0]+0x1a1): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /tmp/cczXnrIF.o: In function at::Tensor c10::KernelFunction::callUnboxed<at::Tensor, c10::ArrayRef, c10::TensorOptions const&>(c10::OperatorHandle const&, c10::ArrayRef, c10::TensorOptions const&) const’:
example-app.cpp:(.text.ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJNS_8ArrayRefIlEERKNS_13TensorOptionsEEEET_RKNS_14OperatorHandleEDpT0[ZNK3c1014KernelFunction11callUnboxedIN2at6TensorEJNS_8ArrayRefIlEERKNS_13TensorOptionsEEEET_RKNS_14OperatorHandleEDpT0]+0x15c): undefined reference to c10::Error::Error(c10::SourceLocation, std::string const&)' /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 c10::Error::Error(c10::SourceLocation, std::string const&)’ follow**
collect2: error: ld returned 1 exit status

What is the problem?

Thanks @glaringlee, for libtorch-cxx11-abi-shared-with-deps-1.7.1.zip, above command is working by setting D_GLIBCXX_USE_CXX11_ABI=1

1 Like