{"id":18754044,"url":"https://github.com/dcavar/torchcpp1","last_synced_at":"2026-04-28T12:02:03.241Z","repository":{"id":150221158,"uuid":"190509654","full_name":"dcavar/TorchCPP1","owner":"dcavar","description":"LibTorch first example based on the online tutorial with functioning CMake configuration","archived":false,"fork":false,"pushed_at":"2019-06-06T03:49:56.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-02-21T12:49:17.983Z","etag":null,"topics":["cmake","cpp11","libtorch","pytorch"],"latest_commit_sha":null,"homepage":"http://damir.cavar.me/","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcavar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-06T03:46:08.000Z","updated_at":"2019-06-09T21:59:08.000Z","dependencies_parsed_at":"2023-04-08T10:37:53.092Z","dependency_job_id":null,"html_url":"https://github.com/dcavar/TorchCPP1","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dcavar/TorchCPP1","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcavar%2FTorchCPP1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcavar%2FTorchCPP1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcavar%2FTorchCPP1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcavar%2FTorchCPP1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcavar","download_url":"https://codeload.github.com/dcavar/TorchCPP1/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcavar%2FTorchCPP1/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32379629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T11:25:28.583Z","status":"ssl_error","status_checked_at":"2026-04-28T11:25:05.435Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","cpp11","libtorch","pytorch"],"created_at":"2024-11-07T17:27:54.674Z","updated_at":"2026-04-28T12:02:03.211Z","avatar_url":"https://github.com/dcavar.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C++ example 1 for PyTorch/LibTorch\n\nby [Damir Cavar], June 2019\n\n\n\n# Introduction\n\nUsing PyTorch or LibTorch with C++ comes with some minor complications. This example solves some of the issues.\n\nFollow the instructions at: [Installing C++ Distributions of PyTorch](https://pytorch.org/cppdocs/installing.html)\n\nYou might face two problems:\n\n- You cannot find `torch/torch.h` and you get compiler errors.\n- CMake does not find the LibTorch config files\n\nIn the following, I will provide solutions that use the path of *LibTorch* relative to my home directory, indicated by the `~`. I also use simply the CPU-version of *LibTorch*.\n\nThe location of `torch/torch.h` is in the folder:\n\n    ~/libtorch/include/torch/csrc/api/include/\n\nThis folder would be added to the `$TORCH_INCLUDE_DIRS` variable by CMake, if correctly configured.\n\nTo allow CMake to find this configuration file, make sure that this line in your CMakeLists.txt points to the directory `~/libtorch/share/cmake/Torch/`:\n\n    set(CMAKE_PREFIX_PATH \"~/libtorch/share/cmake/Torch\")\n\nThe complete *CMakeLists.txt* in this example is:\n\n    cmake_minimum_required(VERSION 3.14)\n    project(TorchCPP1)\n    \n    set(CMAKE_CXX_STANDARD 11)\n    \n    set(CMAKE_PREFIX_PATH \"~/libtorch/share/cmake/Torch\")\n    find_package(Torch REQUIRED)\n    \n    add_executable(TorchCPP1 main.cpp)\n    target_link_libraries(TorchCPP1 \"${TORCH_LIBRARIES}\")\n    target_include_directories(TorchCPP1 PRIVATE ${TORCH_LIBRARIES})\n    set_property(TARGET TorchCPP1 PROPERTY CXX_STANDARD 11)\n \nThe C++ source is taken from [Installing C++ Distributions of PyTorch](https://pytorch.org/cppdocs/installing.html). To compile the code, create a *build* folder:\n\n    mkdir build\n\nChange into this folder:\n\n    cd build\n\nLet CMake generate the make-file by providing an extra parameter that points to *libtorch*, in this case, as mentioned, in my home directory:\n\n    cmake -DCMAKE_PREFIX_PATH=~/libtorch ..\n\nNow compile the code:\n\n    make\n\nYou should see a binary *TorchCPP1* in the directory. Running the binary `./TorchCPP1` should show some output similar to the following:\n\n     0.5393  0.5165  0.8186\n     0.8735  0.4660  0.6906\n    [ Variable[CPUFloatType]{2,3} ]\n\n\n\n[Damir Cavar]: http://damir.cavar.me/ \"Damir Cavar\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcavar%2Ftorchcpp1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcavar%2Ftorchcpp1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcavar%2Ftorchcpp1/lists"}