1. qt 下载地址: http://download.qt.io/archive/qt/5.12/
以前版本的qt 安装可以不登录账号,这次没看到skip按钮,登陆下账号然后正常安装即可,选择下 desktop gcc,其他的组件根据自己需求。
2. libtorch下载地址:https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.5.1%2Bcpu.zip
下载之后解压即可
3. qt 创建 non-qt project C++工程,由于是创新CMake工程,如果没有安装需要安装下。
工程命名为libtorch
main.cpp
#include <iostream>
#include <torch/torch.h>
using namespace std;
int main()
{
torch::Tensor tensor = torch::eye(3);
std::cout << tensor << std::endl;
cout << "Hello World!" << endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(libtorch LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Torch_DIR /home/whq/Downloads/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
add_executable(libtorch main.cpp)
target_link_libraries(libtorch "${TORCH_LIBRARIES}")
set_property(TARGET libtorch PROPERTY CXX_STANDARD 17)
运行结果: