Libtorch with pure cuda codes cannot compiled

OS : win10 64bits
libtorch : 1.9.0+cu102
compiler : vc2019
cuda: 10.2

There are 4 files, CMakeLists.txt, main.cpp, gpu.hpp, gpu.cpp. It is fine it I include torch/torch.h in main.cpp, but the compiler always tell me “Cannot open include file: ‘torch/torch.h’” if I declare torch.h in gpu.hpp.

CMakeLists.txt

cmake_minimum_required(VERSION 3.5.1)

message(STATUS "CMake version: ${CMAKE_VERSION}")

project(diana)

option(USE_CUDA "Use CUDA" ON)

file(GLOB cpu_source_files "${CMAKE_SOURCE_DIR}/src/*.cc")

file(GLOB gpu_source_files "${CMAKE_SOURCE_DIR}/src/*.cu")

set(3RDLIBS_PATH ${CMAKE_SOURCE_DIR}/../../3rdLibs)

set(LIBTORCH_PATH ${3RDLIBS_PATH}/libtorch/libtorch-win-shared-with-deps-1.9.0+cu102/libtorch)

set(CMAKE_PREFIX_PATH "${LIBTORCH_PATH};${CMAKE_PREFIX_PATH};")

add_executable(diana ${cpu_source_files})

find_package(CUDA 10.2)

find_package(Torch REQUIRED)

CUDA_ADD_LIBRARY(diana_gpu ${gpu_source_files})

target_link_libraries(diana diana_gpu ${TORCH_LIBRARIES})

main.cc

#include <torch/torch.h> //works fine if I declare the header in main.cpp

#include <iostream>

#ifdef USE_CUDA
#include "gpu.hpp"
#endif

int main()
{
    std::cout << "Hello, world!" << std::endl;

    printCudaVersion();

    return 0;
}

gpu.hpp

//#include <torch/torch.h> //this will cause error message "Cannot open include file: 'torch/torch.h'"

void printCudaVersion();

gpu.cu

#include <iostream>
#include "gpu.hpp"

void printCudaVersion()
{
    std::cout << "CUDA Compiled version: " << __CUDACC_VER__ << std::endl;

    int runtime_ver;
    cudaRuntimeGetVersion(&runtime_ver);
    std::cout << "CUDA Runtime version: " << runtime_ver << std::endl;

    int driver_ver;
    cudaDriverGetVersion(&driver_ver);
    std::cout << "CUDA Driver version: " << driver_ver << std::endl;
}