Compiler problems with .cuh and .cu files

Having a problem compiling, when I put my kernel and launch function into .cuh and .cu files.

kernels.cuh

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

enum kernelProp
{
	_gridDim,
	_blockDim,
	_threadID,
	_blockID,
};


__global__
void kernel(int* dev_out, kernelProp kProp);
int launchKernel(int dim);

kernels.cu

#include "kernels.cuh"



__global__
void kernel(int* dev_out, kernelProp kProp)
{
	if (kProp == _gridDim)
		dev_out[0] = gridDim.x;
	if (kProp == _blockDim)
		dev_out[0] = blockDim.x;
	if (kProp == _threadID)
		dev_out[0] = threadIdx.x;
	if (kProp == _blockID)
		dev_out[0] = blockIdx.x;
}

int launchKernel(int dim)
{
	int* dev_out;
	int out;
	HANDLE_ERROR( cudaMalloc(&dev_out,sizeof(int)) );
	dim3 grid(dim);
	kernel<<<grid,3>>>( dev_out, _blockID );
	HANDLE_ERROR( cudaMemcpy(&out,dev_out,sizeof(int),cudaMemcpyDeviceToHost) );
	return out;
}

void cleanUp()
{
	
}

main.cpp

#include <iostream>

#include "kernels.cuh"

using std::cout;

#define DIM 1000



int main()
{
	int var;
	var = launchKernel(DIM);
	cout << var;
	
	return 0;
}

Don’t know what to do with ‘…/Documents/Visual Studio 2010/OpenGLStuff/Particles GPU/kernel.cuh’

Well is it kernels.cuh or kernel.cuh ? Perhaps you should post the complete compile error output from VS.

its kernels.cuh and kernels.cu.

I changed some stuff in my build. In the SDK examples I noticed, that all the .cuh and .cu are excluded from the build. So I did that as well. Now I’m getting linker errors:

main.obj : error LNK2001: unresolved external symbol _gridDim
1>main.obj : error LNK2001: unresolved external symbol _threadIdx
1>main.obj : error LNK2001: unresolved external symbol _blockDim
1>main.obj : error LNK2001: unresolved external symbol _blockIdx
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl launchKernel(int)" (?launchKernel@@YAHH@Z) referenced in function _main
1>\Documents\Visual Studio 2010\CUDAStuff\Debug\CUDATestExternCuh.exe : fatal error LNK1120: 5 unresolved externals

the unresolved external symbol is referring to my enum kernelProp. What am I doing wrong?

You don’t exclude .cu files from the build, normally. Those are considered to be compilable modules, ordinarily. A .cuh is a header file, it should be excluded from the build, it only gets compiled when it is included in a compilable module somewhere.

I finally got it. Thanks x100 for the help!

The linker errors were a result of including headers from the “Cuda by Example” headers.

Just as txbob said, you have to exclude the .cuh files from the build.