nvlink error with shared variable


I’m trying to create an object in shared memory but I’m getting the following error

nvlink error : Undefined reference to ‘tt’ in ‘./MyClass.o’
make: *** [MyProject] Error 255

I’m creating the object in my Kernel in the following way

global void myKernelFunction(inputs)
extern shared MyObject tt;
tt = MyObject(input);

//rest of code

I’m also allocating shared memory in the kernel call with

//some code

myKernelFunction<<<numBlocks, numThreads, sizeof(MyObject)>>>(inputs);

//more code

Note that even this won’t work:

extern shared int sdata;

Per the documentation, use the array (i.e. ) syntax:


extern shared MyObject tt;

tt[0] = MyObject(inputs);

For example, this compiles successfully for me:

class MyObject {
  int idata;
  float fdata;

__host__ __device__
  idata = 0;
  fdata = 0.0f;
__host__ __device__
MyObject(int _idata){
  idata = _idata;
  fdata = 0.0f;
__global__ void kernel() {
  extern __shared__ MyObject tt[];
  tt[0] =MyObject(0);

int main()
  kernel<<<1,1, sizeof(MyObject)>>>();
  return 0;

the fixed it. thanks