Cannot include .cu in a .cu file

I have a radix_sort.cu file that includes a prefix_sum.cu file. In prefix_sum.cu I have a function called CHECK_ERROR.

I get the following errors:

Error 1 error LNK2005: “void __cdecl CHECK_ERROR(void)” (?CHECK_ERROR@@YAXXZ) already defined in prefix_sum.cu.obj radix_sort.cu.obj

Error 2 fatal error LNK1169: one or more multiply defined symbols found G:\School\MasterThesis\CollisionDetection\CUDACD\CUDACD\x64\Release\CUDACD.exe 1

But I only have one instance of CHECK_ERROR in prefix_sum.cu. When I comment the function the compilation works fine. I seems like the compiler sees 2 instances of CHECK_ERROR function because I have a include statement of prefix_sum.cu in radix_sort.cu

My code:

prefix_sum.cu

#include <math.h>

#include <stdio.h>

#include <cuda.h>

#include "VecTypes.cu"

...

//HELPER FUNCTIONS

//just printf errors that has occurred

__host__ void CHECK_ERROR()

{

	//cudaGetLastError() resets to cudaSuccess

	cudaError err = cudaGetLastError();

	switch(err)

	{

		case cudaSuccess:

			printf("No error.\n");

			break;

		case cudaErrorMissingConfiguration :

			printf("Missing configuration error\n");

			break;

		case cudaErrorMemoryAllocation :

			printf("Memory allocation error\n");

			break;

		case cudaErrorInitializationError :

			printf("Initialization error\n");

			break;

		case cudaErrorLaunchFailure :

			printf("Launch failure\n");

			break;

		case cudaErrorPriorLaunchFailure :

			printf("Prior launch failure.\n");

			break;

		case cudaErrorLaunchTimeout :

			printf("Launch timeout error.\n");

			break;

		case cudaErrorLaunchOutOfResources :

			printf("Launch out of resources error.\n");

			break;

		case cudaErrorInvalidDeviceFunction :

			printf("Invalid device function.\n");

			break;

		case cudaErrorInvalidConfiguration :

			printf("Invalid configuration.\n");

			break;

		case cudaErrorInvalidDevice :

			printf("Invalid device. \n");

			break;

		case cudaErrorInvalidValue :

			printf("Invalid value. \n");

			break;

		case cudaErrorInvalidPitchValue :

			printf("Invalid pitch value. \n");

			break;

		case cudaErrorInvalidSymbol  :

			printf("Invalid symbol. \n");

			break;

		case cudaErrorMapBufferObjectFailed  :

			printf("Map buffer object failed.\n");

			break;

		case cudaErrorUnmapBufferObjectFailed  :

			printf("Unmap buffer object failed.\n");

			break;

		case cudaErrorInvalidHostPointer  :

			printf("Invalid host pointer.\n");

			break;

		case cudaErrorInvalidDevicePointer  :

			printf("Invalid device pointer.\n");

			break;

		case cudaErrorInvalidTexture  :

			printf("Invalid texture.\n");

			break;

		case cudaErrorInvalidTextureBinding  :

			printf("Invalid texture binding.\n");

			break;

		case cudaErrorInvalidChannelDescriptor  :

			printf("Invalid channel descriptor.\n");

			break;

		case cudaErrorInvalidMemcpyDirection  :

			printf("Invalid memcpy direction.");

			break;

		case cudaErrorAddressOfConstant  :

			printf("Address of constant error.\n");

			break;

		case cudaErrorTextureFetchFailed  :

			printf("Texture fetch failed.\n");

			break;

		case cudaErrorTextureNotBound  :

			printf("Texture not bound error. \n");

			break;

		case cudaErrorSynchronizationError  :

			printf("Synchronization error\n");

			break;

		case cudaErrorInvalidFilterSetting  :

			printf("Invalid filter setting. \n");

			break;

		case cudaErrorInvalidNormSetting  :

			printf("Invalid norm setting. \n");

			break;

		case cudaErrorMixedDeviceExecution  :

			printf("Mixed device execution.\n");

			break;

		case cudaErrorCudartUnloading  :

			printf("CUDA runtime unloading.\n");

			break;

		case cudaErrorUnknown  :

			printf("Unknown error condition.\n");

			break;

		case cudaErrorNotYetImplemented  :

			printf("Function not yet implemented.\n");

			break;

		case cudaErrorMemoryValueTooLarge   :

			printf("Memory value too large.\n");

			break;

		case cudaErrorInvalidResourceHandle   :

			printf("Invalid resource handle.\n");

			break;

		case cudaErrorNotReady   :

			printf("Not ready error.\n");

			break;

		case cudaErrorInsufficientDriver   :

			printf("CUDA runtime is newer than driver.\n");

			break;

		case cudaErrorSetOnActiveProcess   :

			printf("Set on active process error.\n");

			break;

		case cudaErrorNoDevice   :

			printf("No available CUDA device.\n");

			break;

		case cudaErrorStartupFailure   :

			printf("Startup failure\n");

			break;

		case cudaErrorApiFailureBase   :

			printf("API failure base.\n");

			break;

		default:

			printf("No case to catch: Unknown Error.\n");

			break;

	}

}

in radix_sort.cu I include prefix_sum.cu

#include <prefix_sum.cu>

...

Does anyone know why I get this error?

Yes, when you include prefix_sum.cu.cu in your radix_sort.cu, the compiler will find two definitions of CHECK_ERROR() and thus you will get an error.

You solve it by creating a .h file with the declaration of CHECK_ERROR() (don’t forget the include guards) and then include the header in radix_sort.cu.

See also http://en.wikipedia.org/wiki/One_Definition_Rule

Yes, when you include prefix_sum.cu.cu in your radix_sort.cu, the compiler will find two definitions of CHECK_ERROR() and thus you will get an error.

You solve it by creating a .h file with the declaration of CHECK_ERROR() (don’t forget the include guards) and then include the header in radix_sort.cu.

See also http://en.wikipedia.org/wiki/One_Definition_Rule