shaveenk
shaveenk

Reputation: 2083

adding a cuda file to an existing c project in visual studio

I am trying to add a CUDA file to my existing C++ Visual Studio project. I have the CUDA 5.0 SDK installed, I have created a new .cu file and also set its Item Type to CUDA/C++ in the CUDA file properties. But it looks like it just does not compile giving errors that say that the compiler does not recognize the CUDA keywords. One of the errors I get is:

error C2065: 'threadIdx' : undeclared identifier

Any suggestions?

Upvotes: 11

Views: 19136

Answers (3)

ejectamenta
ejectamenta

Reputation: 1087

I found that the best way to do this is to perform the following in the existing CPU project

1) Build Dependencies -> Build Customisations

click the Cuda checkbox

2) Create a new simple CUDA project using the wizard (you anyway probably want to test your CUDA project builds ok firstly), load both projects into the IDE and then compare settings between the two projects, you will need to add the following in project settings

$(CudaToolkitLibDir) to additional libraries settings (linker tab) $(CudaToolkitIncludeDir) to additional include directories (c++ tab)

cudart.lib to additional dependencies (linker tab)

Then compare the CUDA tabs

I found that 32 bit had been pre-selected for the target machine architecture for some reason so I changed that to 64 bit.

After this I added a define _CUDA_CODE_COMPILE_ to preprocessor definitions to switch between CUDA or CPU compilation.

#ifdef _CUDA_CODE_COMPILE_
    cudaCodeFunction();
#else
    cpuCodeFunction();
#endif

Not ideal but necessary since there seem to be no defines set to indicate that NVCC is installed (other than performing a shell command!)

Upvotes: 8

Pixelchemist
Pixelchemist

Reputation: 24946

I can't go through it all at the moment but I think those steps are necessary:

  1. Right click on your Project in the Project Explorer Build...(customization?) [my Version is German. I can't tell the keyword exactly but it's something about "Build...". You need to check "CUDA 5.0" here.
  2. Set up the "Additional Include Directories" for Cuda in the "General" Tab of the Compiler options (Project Properties).
  3. Add the cuda libfile to "Additional Dependencies" in the "Input" Tab of the Linker.
  4. Mark the File as Cuda file (you've done that).

Upvotes: 6

fabmilo
fabmilo

Reputation: 48330

You have to select the right Compiler for the .cu files

Are you following any of the tutorial on how to setup it on visual studio ?

http://blog.norture.com/2012/10/gpu-parallel-programming-in-vs2012-with-nvidia-cuda/

Upvotes: 0

Related Questions