I’m trying to compile a piece of .cu code for both device and host using the CUDACC preprocessor variable. With the code below my error message tells me that blockDim is an undeclared identifier. I have tried inserting this code also into some cuda examples (incase it is my project settings), but no luck. Am I barking up the wrong tree, or should I hopefully be able to get such a technique to work?



__device__ __host__ int test_both() {

	int x = 1234567;

#ifdef __CUDACC__

	x = blockDim.x;


	return x;


Well it’s true this question has been asked before (defines, automatically defined by NVCC?), and the answer was NO; but I’m still convinced that somehow it must be possible. I’ve now tried CUDACC,WIN32, and CUDAFE, plus my own macros with -D and -U, here and there, before and after -Xcompiler, but with no luck.

Surely there’s a way. Perhaps a sneaky undocumented macro that pops up just at the right moment?

A single function, with one name, can accept device and host together, so why truly can’t this be done?


Maybe it’s the CUDABE (“CUDA Back End”) macro you have to check against. (Check file <crt/func_macro.h> for an example.)


Thanks, but unfortunately CUDABE is the same as the others. If I test there that it is defined, it compiles (because it’s not defined). Then when I run the program, the results show that the conditional section has been omitted.


I do this and it works fine. I just pass a preprocessor define to nvcc using -D. I use the command line nvcc.exe -cubin -DMYPREPROC …
I am using the driver API so maybe it won’t work with the runtime API?

EDIT: I should also mention that in my code I don’t use blockDim or host, device. Maybe there is a problem there.


Hi DevMike,

I’m not using the driver API, so I can’t comment there; it’s not an option for me in any case. And I also define my own macros like that. But yes, it’s the built-in variables which are the problem - with non-emulator builds anyway.