[SOLVED] Including a .cuh file from a .cpp file, using multiple compilers

Hello !

I have a .cpp, compiled with the MSVC c++ compiler, which includes a .cuh file. The cuh file defines a templated class and calls a kernel. (So the definition is indeed in the .cuh file)

My cuh file:

#pragma once
#include "cuda_runtime.h"
...

template<typename INPUT, typename OUTPUT>
class OffsetMapper : public ZMapper<INPUT, OUTPUT> {

...
OffsetMapperKernel<INPUT, OUTPUT> << < matOutput->dimGrid, matOutput->dimBlock >> > (
...
}

If the .cpp file is compiled with MSVC and I indicate the the .cuh is a CUDA file, compilation fails (syntax error “<”).

I really must flag my cpp file for compilation with nvcc (making it a .cu file, essentially) for it to work. Is that expected behaviour ? I can imagine it is, because the .cuh file gets compiled together with the .cpp file. But is there a workaround ?

Including a .cuh with a declaration and having an equivalent .cu file for the definition works perfectly well - when called from a .cpp file that is compiled with the MSVC toolchain.

Thanks so much your help !

nvcc defines a preprocessor macro named NVCC . You could use it do hide the kernels from the host compiler.

Another possiblity is that you use ordinary c++ wrapper functions to call the kernels.

Something like

//in kernels.cuh
void callMyKernel(params);

//in kernels.cu

__global__
void myKernel(...){

}

void callMyKernel(params){
    myKernel<<<grid, block>>>(...);
}

Yeah ok the second solution makes sense.

Hiding my kernel launch behind NVCC will compile but obviously not work…

Anyways, thanks so much for your help !