├── package.xml ├── CMakeLists.txt └── README.md /package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | torch_cpp 4 | 2.7.1 5 | torch C++ library 6 | 7 | Christian Rauch 8 | 9 | MIT 10 | 11 | cmake 12 | 13 | 14 | cmake 15 | 16 | 17 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | project(torch_cpp CXX) 3 | 4 | set(TORCH_URL https://download.pytorch.org/libtorch/cu126/libtorch-cxx11-abi-shared-with-deps-2.7.1%2Bcu126.zip) 5 | set(TORCH_URL_MD5 f3f9bdc2fcaada6e224b0c2efb50d29e) 6 | 7 | include(ExternalProject) 8 | ExternalProject_Add(torch_archive 9 | URL ${TORCH_URL} 10 | URL_MD5 ${TORCH_URL_MD5} 11 | PREFIX torch_archive 12 | CONFIGURE_COMMAND "" 13 | BUILD_COMMAND "" 14 | INSTALL_COMMAND "" 15 | ) 16 | 17 | ExternalProject_Get_Property(torch_archive SOURCE_DIR) 18 | 19 | set(TORCH_PATH ${SOURCE_DIR}) 20 | 21 | 22 | # manually install header, libraries and config files 23 | 24 | install(DIRECTORY ${TORCH_PATH}/include/ 25 | DESTINATION include 26 | FILES_MATCHING PATTERN "*.h*" 27 | ) 28 | 29 | install(DIRECTORY ${TORCH_PATH}/lib/ 30 | DESTINATION lib 31 | ) 32 | 33 | install(DIRECTORY ${TORCH_PATH}/share/ 34 | DESTINATION share 35 | ) 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LibTorch install script for CMake 2 | 3 | This CMake project is basically an install script for LibTorch (PyTorch C++ API). It downloads the binary archieve and installs headers, libraries and CMake configurations files to the corresponding `INCLUDEDIR`, `LIBDIR` and `DATADIR` directories. 4 | 5 | The package can be used as part of a pure CMake catkin or colcon workspace by including: 6 | ```XML 7 | cmake 8 | torch_cpp 9 | 10 | cmake 11 | 12 | ``` 13 | in your `package.xml`. The relevant files will then be installed within the workspace target folder (`install` or `devel`). 14 | 15 | Usage in a CMake project: 16 | ```CMake 17 | find_package(Torch REQUIRED) 18 | target_link_libraries(${PROJECT_NAME} PUBLIC torch) 19 | set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14) 20 | target_link_options(${PROJECT_NAME} PUBLIC ${TORCH_CXX_FLAGS}) 21 | ``` 22 | 23 | If you get the error `No CMAKE_CUDA_COMPILER could be found.`, then the CUDA compiler `nvcc` cannot be found in the default search paths (`$PATH`). In this case, you have to set the path to `nvcc` manually: 24 | ```sh 25 | export CUDACXX=/usr/local/cuda/bin/nvcc 26 | ``` 27 | --------------------------------------------------------------------------------