下载libtorch

cd/root/Data/libs/cpp
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
unzip libtorch-shared-with-deps-latest.zip

创建流程

初始化

cd /root/Projects/myProjects/cppProjects
mkdir torchTest && cd torchTest

编写源码example-cpp.cpp

#include <torch/torch.h>
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}

编写CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(torchTest)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(torchTest example-app.cpp)
target_link_libraries(torchTest "${TORCH_LIBRARIES}")
set_property(TARGET torchTest PROPERTY CXX_STANDARD 14)

编译项目

mkdir build && cd build
cmake -DCMAKE_PREFIX_PATH=/root/Data/libs/cpp/libtorch ..
cmake --build . --config Release

如图:

image-20210607153134979

运行项目

image-20210607153256435

参考

评论




博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议

本站使用 Volantis 作为主题,总访问量为
载入天数...载入时分秒...
冀ICP备20001334号