[CUDA3] C++ static members Compilation problem when using C++ static members

I’m trying to use c++ classes in a cuda3 kernel, everything worked fine until I added static members:

Here is the code:

struct B

{

	static int x;	

};

int B::x = 42;

__global__ void kernel()

{

	int test = B::x;

}

The error:

error: identifier "_ZN1B1xE" is undefined

Am I missing something? :unsure:

Thanks

B::x is a host variable, that is, it lives in the host memory space, so it’s illegal to use it from a global function.

I am not sure static class members are allowed in device code. CUDA only allows a (generous, but not complete) subset of C++ features.
Appendix D of the toolkit 3.0 programming guide lists the supported C++ features, but does not mention static class data, so by implication, it’s unsupported.

Even if it was supported, you’d have an interesting question… is the static class data global across the device or local to the block?

Thanks for your answers. I didn’t specify that I needed a const static member.

But even with this code:

struct B

{

	__constant__ static  const int x;	

};

__constant__  const int B::x = 42;

__global__ void kernel()

{

	int test = B::x;

}

I got this error

error C2720: 'B::x__cuda_shadow_variable__'Â :  'static ' not valid on member.

I finally got it working by moving the const static member as a const global variable.