c++cuda

texture is not a template


I am using CUDA in Microsoft Visual Studio 2019 for this code:

#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <cuda.h>
#include <device_launch_parameters.h>
#include <fstream>
#include <cstddef>

texture<float, 2, cudaReadModeElementType> texRef;

It gives me texture is not a template error, and there seems to be absolutely nothing I can done in that. All the other CUDA-related code I have tried seemed to compile ok. The file is indeed a .cu file, not a .cpp file. The CUDA Samples with textures compile correctly for me.

What can possibly be the issue here?

(While there are the same questions, they all suggest that changing a .cpp file to a .cu file should help, which does not seem to be my case.)


Solution

  • It's because your project is trying to compile your file with Cl, not nvcc.

    Replace in your .vcxproj project file

    <ClCompile Include="myfile.cu" />`
    

    by

    <CudaCompile Include="myfile.cu" />
    

    If you updated cuda, you also need to update your ExtensionSettings in project files:

      <ImportGroup Label="ExtensionSettings">
        <Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 11.3.props" />
      </ImportGroup>
    

    I add that texture references are deprecated in cuda 11.3 and instead you should use texture objects.

    One can also get an error message while using nvcc with cuda 12+, due to depreciation (cuda 11) and removal (cuda 12) of this syntax; solution here.