compilation of device_launch_parameters.h and curand_kernel.h together produces errors related to C+

In my cuda-c++ project, I am using cuRAND library for generating random numbers and I have included below files in my header file:

// included files in header file
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <curand_kernel.h>  // cuRAND lib
#include "device_launch_parameters.h"

I am able to compile my project on Windows 7 and Windows 8 with no errors. But when I tried to compile it on linux with g++ (GCC) 4.4.7 (Red Hat 4.4.7-16), I am getting below error:

/usr/local/cuda-7.5/bin/../targets/x86_64-linux/include/curand_mtgp32_kernel.h:122: error: previous declaration of ‘const uint3 threadIdx’ with ‘C++’ linkage
/usr/local/cuda-7.5/bin/../targets/x86_64-linux/include/device_launch_parameters.h:71: error: conflicts with new declaration with ‘C’ linkage
/usr/local/cuda-7.5/bin/../targets/x86_64-linux/include/curand_mtgp32_kernel.h:121: error: previous declaration of ‘const dim3 blockDim’ with ‘C++’ linkage
/usr/local/cuda-7.5/bin/../targets/x86_64-linux/include/device_launch_parameters.h:73: error: conflicts with new declaration with ‘C’ linkage
make: *** [host] Error 1

I can see “threadIdx” and “blockDim” declared as “extern” in “curand_mtgp32_kernel.h” and actual definitions in “device_launch_parameters.h”.

I am not sure what this error means and how should I resolve it?

The correct answer was already given in one of the comments on your cross-posting on stackoverflow.

I’m not sure where your stackoverflow question is located. Can you please tell me how you solved this issue? I am currently having the same issue.

I am posting to this thread since it is still the top and most relevant (Google) search result for the error message.

The most relevant stackoverflow answer I could find was this one

For me, the problem came about because “curand_mtgp32_kernel.h” declares threadIdx and blockDim, and “device_launch_parameters.h” also declare threadIdx and blockDim, this time in an ifdef C++ extern C block.

The stackoverflow answer noted that if the linkage was not specified, C++ was assumed, and redeclaring it without specifying a linkage would use the previous linkage, but not the other way around. They referenced the cppreference language linkage.

So, my fix was to #include <device_launch_parameters.h> before #include <curand.h> and #include <curand_kernel.h>