CUDA in C++ .cpp files

Hello all,
I have started learning to use CUDA with the CUDA by example book. I have it all set up and working with MSVC 2008 c++ and compiles .cu files perfectly…

The book keeps mentioning that you can develop in C++ and in the SDK examples from the nvidia site, I can see .cpp files with cuda code in them, such as the line:
’ cudaResult = cudaMalloc((void **)&m_d_samples, m_nSamplesBatchTarget * sizeof(float)); ’
in the rng.cpp file in the ‘randomFog’ example.

Unfortunately if I set my .cu file to .cpp and make sure the custom nvidia build rule is still being used, i still get errors like:
’ error C3861: ‘cudaMalloc’: identifier not found ’
which shows that its not really working.

Any tips on any other steps i need to perform? I can never get the sdk examples to open correctly to experiment with them.
Thanks in advance,

try to use msvs for c++ files, not cuda build rule. but include some cuda headers and libs, so you can call cuda functions from cpp files.

That fixed 99% of the issues. Unfortunately it doesnt like the following lines that were found in the code

int tid = threadIdx.x + blockIdx.x * blockDim.x;

(doesnt like the threadId or blockId or blockDim, which also then leads to problems with the .x)


add<<<(N+127)/ 128, 128>>>(dev_a, dev_b, dev_c, dev_n);

(doesnt like the ‘<’ )

any ideas how i should replace them or am i still missing a header file?

i have:


#include <time.h>

#include <windows.h>

#include <cutil_inline.h>

#include <cutil_gl_inline.h>

#include <curand.h>




#include <cudaGL.h>

#include <cuda.h>

some of which are probably overkill or not used.

You cannot call kernells directly from cpp file, need to make wrapper function.