Unresolved externals when using thrust

Using thrust functions is giving unresolved externals in all of them. I already included

cuda/include, libcudacxx/include, thrust, cub which are all the includes in the samples. I also linked with cudart.lib, cudart_static.lib. cudadevrt.lib. I don’t know what else to include?

In the files I included all the .h files where these functions are, e.g. transform_iterator.h, copy.h, etc.

here are the errors:

unresolved external symbol “class thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::transform_iterator<struct get_color_ref,class thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::detail::normal_iterator,struct thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::use_default,struct thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::use_default> __cdecl thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::cuda_cub::copy<struct thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::cuda_cub::tag,class thrust::THRUST_300104_SM___CUDA_ARCH_LIST___NS::detail::normal_iterator,class

Thrust is a header-only library and should not require any linking. Please show a minimal reproducer code and the used compilation command.

I believe your compiler setup is not correct. When correctly compiled with NVCC, the macro __CUDA_ARCH_LIST__ should be expanded to a list of gpu architecture.

For example, this code

#include <thrust/device_vector.h>
#include <iostream>

int main(){
    std::cout << typeid(thrust::device_vector<int>{}).name() << "\n";
}

prints N6thrust16_V_300300_SM_90013device_vectorIiNS0_16device_allocatorIiEEEE when compiled with nvcc -arch=sm_90 , and N6thrust16_V_300300_SM_89013device_vectorIiNS0_16device_allocatorIiEEE when compiled with nvcc -arch=sm_89

(note __CUDA_ARCH_LIST__ was expanded to 900 and 890, respectively)

Thanks for the reply. I have been having all kinds of problems with Cuda code which lends me to agree with you that the cuda compiler might be the problem.

Here is how I am using thrust:

	thrust::device_vector<Vertex> d_vertices = m_singleFrameImages[i]->getImageVertices();

	// copy the image pixels to a device vector, use the constructor do not copy
	thrust::device_vector<uchar> d_pixels(m_denoisedImagesPixels[i].get(), m_denoisedImagesPixels[i].get() + (imagesWidth * imagesHeight));

	// Create a fancy iterator that points to the 'color' members of d_vertices
	thrust::transform_iterator<get_color_ref, thrust::device_vector<Vertex>::iterator> x_iter(d_vertices.begin(), get_color_ref());

	// Copy the values from d_pixels into the 'color' members using thrust::copy
	thrust::copy(d_pixels.begin(), d_pixels.end(), x_iter);


I am setting a value in a vector of structs from another vector by using a function (get_color_vector) to do the copying.

I am using Visual studio so I am not compiling on the command line. Here is the command from the log file:

HostCommandLineTemplate = -Xcompiler “/EHsc [Warning] /nologo [Optimization] /FS [DebugInformationFormat] [RuntimeChecks] [Runtime] [TypeInfo]” -Xcompiler “/Fd[ProgramDataBaseFileName]”
SplitCompile = Default
CodeGeneration = compute_86,sm_86;compute_89,sm_89;compute_120,sm_120
AdditionalCompilerOptions =
EnableVirtualArchInFatbin = true
RuntimeApiCommandLineTemplate = -ccbin “C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64” -x cu [GenerateRelocatableDeviceCode] [ExtensibleWholeProgramCompilation] [Include] [RequiredIncludes] [InterleaveSourceInPTX] [GPUDebugInfo] [GenerateLineInfo] [Keep] [KeepDir] [FastMath] [MaxRegCount] [PtxAsOptionV] [SplitCompile] [FastCompile] [TargetMachinePlatform] [NvccCompilation] [CudaRuntime] [AdditionalOptions] [HostDebugInfo] [Emulation] [Defines] -Xcompiler “/EHsc [Warning] /nologo [Optimization] /FS [DebugInformationFormat] [RuntimeChecks] [Runtime] [TypeInfo]” -Xcompiler “/Fd[ProgramDataBaseFileName]” [CompileOut] “%(FullPath)”

BuildCommandLineTemplate = --use-local-env
GenerateRelocatableDeviceCode = true
UseHostDefines = true
GPUDebugInfo = true
Xcompiler=“/EHsc -Zi -Ob0”
Defines = ;_WINDOWS;CMAKE_INTDIR=“Debug”;Zc:preprocessor
CudaRuntime = Static
NvccCompilation = compile
PropsCacheOutputFile = %(Filename)%(Extension).cache
TargetMachinePlatform = 64
CommandLineTemplate =

               # Driver API (NVCC Compilation Type is .cubin, .gpu, or .ptx)
               set CUDAFE_FLAGS=--sdk_dir "C:\Program Files (x86)\Windows Kits\10\"
               "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\nvcc.exe" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64" -x cu [GenerateRelocatableDeviceCode] [ExtensibleWholeProgramCompilation] [Include] [RequiredIncludes] [InterleaveSourceInPTX] [GPUDebugInfo] [GenerateLineInfo] [Keep] [KeepDir] [FastMath] [MaxRegCount] [PtxAsOptionV] [SplitCompile] [FastCompile] [TargetMachinePlatform] [NvccCompilation] [CudaRuntime] [AdditionalOptions] [Defines] [CompileOut] "%(FullPath)"
               
               # Runtime API (NVCC Compilation Type is hybrid object or .c file)
               set CUDAFE_FLAGS=--sdk_dir "C:\Program Files (x86)\Windows Kits\10\"
               "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\nvcc.exe" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64" -x cu [GenerateRelocatableDeviceCode] [ExtensibleWholeProgramCompilation] [Include] [RequiredIncludes] [InterleaveSourceInPTX] [GPUDebugInfo] [GenerateLineInfo] [Keep] [KeepDir] [FastMath] [MaxRegCount] [PtxAsOptionV] [SplitCompile] [FastCompile] [TargetMachinePlatform] [NvccCompilation] [CudaRuntime] [AdditionalOptions] [HostDebugInfo] [Emulation] [Defines] -Xcompiler "/EHsc [Warning] /nologo [Optimization] /FS [DebugInformationFormat] [RuntimeChecks] [Runtime] [TypeInfo]" -Xcompiler "/Fd[ProgramDataBaseFileName]" [CompileOut] "%(FullPath)"

Here is the command line from the linker page:

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\nvcc.exe” -dlink --dlink-time-opt -o MRIViewer\x64\Debug\MRIViewer.device-link.obj -Xcompiler “/EHsc /nologo /Zi " -Xcompiler “/Fd[ProgramDataBaseFileName]” -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\x64/crt” -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\lib\x64" -G

If I compile each file in the project separately I do not get any errors, but when I compile the project it generates the unresolved external error.

does that help show if the problems are coming from the cuda compiler?

Thanks

You could try compiling without device debugging (remove flag -G)

You could try compiling the small cuda code I posted above in a fresh project.

Also, the CUDA installation guide for windows recommends verifying the CUDA / VS setup using the provided cuda samples. They come with VS solution files, and the document also explains how to add cuda configuration to an existing project.

I don’t have more suggestions. I do not develop on windows.

Thank you for your help. I appreciate it.