I declare a template function in main.h , define and instantiate it in main.cu, and call it in main.cpp. When I use thrust::complex<float> to instantiate and call the function, I got a link error.
main.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl test<struct thrust::THRUST_200802_SM___CUDA_ARCH_LIST___NS::complex<float> >(struct thrust::THRUST_200802_SM___CUDA_ARCH_LIST___NS::complex<float>)" (??$test@U?$complex@M@THRUST_200802_SM___CUDA_ARCH_LIST___NS@thrust@@@@YAXU?$complex@M@THRUST_200802_SM___CUDA_ARCH_LIST___NS@thrust@@@Z) referenced in function main
My codes are like this
//main.h
#pragma once
template <typename T>
void test(T a);
//main.cpp
#include "main.h"
#include <thrust/complex.h>
int main() {
thrust::complex<float> c1(1.0f, 2.0f);
test(c1); // this line cause the link error
float c2;
test(c2);
}
//main.cu
#include "main.h"
#include <thrust/complex.h>
template <typename T>
void test(T a) {}
template void test<int>(int a);
template void test<float>(float a);
template void test<thrust::complex<float>>(thrust::complex<float> a);
I’m using:
CUDA 12.9
MSVC 2022
CMake 4.0.3
Here are the codes in cmake
cmake_minimum_required(VERSION 3.27)
project(CUDATemplates)
enable_language(CUDA)
add_executable(CUDATemplates main.cpp main.h main.cu)