Now that CUDA Toolkit 3.2 was released, the integration with Visual Studio 2008 is a lot easier than before. In this tutorial, we will see how we can create a CUDA application using Visual Studio 2008!
Getting the free version of Visual Studio 2008
Visual C++ 2008 Express is free to download and use, so if you don’t have Visual Studio 2008 and don’t want to buy it, start by downloading it from here:
When the page is loaded, click the Visual Studio 2008 Express link (so you won’t download VS2010, which is not yet supported):
And from the list, select Visual C++ 2008 Express Edition and click Free Download (80mb):
The installation process might take a while.
CUDA in Visual Studio 2008
Start Visual Studio 2008 and create a new Win32 Console Application (C++) project:
Give it a name, like “CUDAinVS2008”, and click OK. The Win32 Application Wizard will start, click “Next >”. On the next screen, set this to be an Empty project and click Finish:
Next, lets add a .cu file and write a simple CUDA application that we will compile.
Right click on the Project and select Add->New Item…:
Now, you get the “Add New Item” screen. Select the C++ File(.cpp) and name it “CUDAinVs2008.cu”. You can name this anything you want, but remember to give it the surname .cu. If not, you can also rename the file by right clicking the .cpp file in the project tree and select Rename.
Click Add to add the new cu file to our project.
The file should now be opened (blank file), but if now, simply double click it to open it in the editor.
Let’s type in a really simple CUDA C program:
int main(void)
{
return 0;
}
Right now, Visual Studio doesn’t recognize the .cu file, so it’s not possible to compile this. To overcome this, CUDA comes with some custom rules that we can apply to our project. Right click the project and select Custom Build Rules…:
A new dialogue pops up, click “Find Existing…” and browse to the \extras\visual_studio_integration\rules-folder in your CUDA installation directory. On my system, this is located here:
C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v3.2\extras\visual_studio_integration\rules.
From here, select NvCudaDriverApi.v3.2.rules and click Open, do the same again to addthe NvCudaRuntimeApi.v3.2.rules. Next, select these two from the Custom Build Rule File dialogue and press OK.
Now that we set the custom build rule to include the CUDA rules, we just need to include the CUDA library in our project, so we get the CUDA functionality!
Again, right click the project and select Properties.
From the Properties window, select Configuration Properties>Linker>General:
On the Additional Library Directory, add the path to the lib folder in your CUDA installation:
C:\Program Files (x86)\NVIDIA GPU Computing Toolkit\CUDA\v3.2\lib\Win32
Next, select Configuration Properties>Linker>Input and add “cudart.lib” in the Additional Dependencies property:
Next, click OK.
Now, the project should be ready for compiling CUDA C projects. Try to compile it and run the application (press ). You should see it compile, and then the console windows flashes. This is because the application starts and then quits as that’s what we programmed our application to do.
But, we got CUDA running from Visual Studio 2008! Now I suggest creating a new project and doing these steps so you remember it, it might seem a lot but it usually just take a few seconds to set up.
Pingback: Windows Client Developer Roundup 053 for 1/6/2011 - Pete Brown's 10rem.net
Thanks for the tips! I was having trouble getting this to work with Visual Studio 2010. Going to use Visual C++ 2008 Express as you outlined here.
Good that it worked. Please tell me if you get it working in VS2010 🙂
I agree with digital error.
It seems VS2010 does not like those build customization files you mention.
Upon clicking “Build Customizations,” there are two already available, but not the ones you mention.
CUDA 3.1(targets, .props) and CUDA 3.2(targets, .props) .
When you try to add the .rules files, an unknown error pops up.
I also did the other steps after this one, but I still get a “1 unresolved externals” error.
Maybe it does not function in VS2010 because I notice VS2010 wants files with the .target extension while the files you mention have the .rules extension
After doing some search it seems MS changed these to .target files.
Will have to look up to see is nVidia made some new ones for VS2010
If there was already a project solution made, VS2010 would have converted it….maybe
nVidia gives details on VS2010 how to build a project from scratch.
http://forums.nvidia.com/index.php?showtopic=184539
Oh, as a side note…what the directions fail to mention is the compile will still fail because you still need VS2008 or Windows SDK on your box because VS2010 still wants to use the v90 toolset to compile the CUDA code. VS2010 has v100
Thanks for the research. I will take a dive into this as well.
This could be now moot since they released version 4 of CUDA. I have not had the time to check if it is fully VS2010 compatable.
WOW , Great Post
Oh! Thanks a million for the website. It works! This one should have popped to the top of Google search result.
Thank you, very much.
thank you, very much.^^
Which operating system was this guide run on? If it’s 32 bit windows, what do we change for 64 bit Vista?
It does not matter. Visual Studio only comes in 32 bit flavor.
Welcome to a Facebook Page about mine,I’ve learned a lot from your blog here,Keep on going,my friend,I will keep an eye on it,One more thing,thanks for your post!.
cudart.lib does not appear as you write it above (CUDA 4.0)
my mistake, thanks very much, everything works just fine 🙂
nVidia’s Parallel Nsight integrates right into Visual Studio 2010, which allows you to go right into making a project without the need for doing all these adustments.
but does not work with Visual Studio Express 😦
If you are a student, you should be able to get it for free,
Man, i really thank you so much! Its saved my life.
WORKS in VS2008 EXPRESS!
=)
Trying on and off for the last 6 months. Finally got it running. Could you please provide some additional projects as a practice or if you know of some links. Your tutorial is really great !!
Thanks a lot friend!… u saved a lot of time….