when .cu file is included multiple times


I am having a trouble in file linking. Suppose I have a kernel.cu file defined as follows:

#ifndef KERNEL_A
#define _KERNEL_A

global void
kernel( int* g_data )


and then I have two other files that need to include the kernel.cu file:

// func_a.cu:
#include “kernel.cu”


#include “kernel.cu”


When I compile the project in vs 2005, it gives me the error msg:

func_b.obj : error LNK2005: ___device_stub__Z6kernelPi already defined in func_a.obj.

Anyone know how to fix this? Any help is highly appreciated.

I think you should exclude your kernel.cu file from the build (right click on the file on the solution explorer, then properties, and then in general (do that for all compilation modes))

It does look like kernel.cu is excluded. The problem is that both func_a.obj and func_b.obj contain code for kernel() and ODR says: “one too many”. If one of them could only declare ‘void kernel()’ and not define it, then linker might be happier.

In C++, a common header can define an “inline” function, or a template of function. Compilers and linkers cope with that. But with CUDA we have nvcc doing some additional steps, so compiler and linker get something almost but not quite what we write in the source.