Simple CUDA build rule for Visual Studio 2005

I’ve made a simple “build rule” for .cu files for Visual Studio 2005. This makes it so you don’t have to manually define a custom build rule for every CUDA file in your project.

It simply invokes nvcc on the file and forwards the C/C++ options to cl.exe. You can also specify whether or not to compile emulation code.

To install the rule:

right-click on your project in Visual Studio 2005
click Custom Build Rules…
click Find Existing…
browse to cuda.rules
check the box next to the CUDA listing in the Available Rule Files pane
click ok

Now you can compile .cu files in the same way as .c/.cpp.

Unfortunately, I don’t see a way to easily fill in the default options for the different Debug,EmuDebug, etc configurations, so for now you have to do it by hand. The way I dealt with it is to make a template CUDA project, and fill in the defaults. I’ve also attached this template. To use it, you should have CUDA_INC_PATH, CUDA_LIB_PATH, CUDA_BIN_PATH, and CUDA_LIBRARIES defined in your environment.

Extending the rules to support the rest of the nvcc options would be simple but tedious, but if anyone does, please post it here so we can all use it! :)

Great addition – definitely will give it a try – thanks!


Thanks for contributing this build rule, but I’ve been having some problems with it. I set up the paths that I need, including setting $(CUDA_LIBRARIES) to C:\CL, which is where I threw the the lib files. When I go to compile, it goes fine until it hits linking where it throws this error:

LINK : fatal error LNK1104: cannot open file 'C:\CL.obj'

This error is usually related to extra whitespace in a path name, however my path is set to only C:\CL. I tried many variations on this, with no success. I was just wondering if anyone else encountered this problem, and if it actually was related to something else.


CUDA_LIBRARIES is meant to refer to the actual filenames of the libraries, not some search path.

For instance, here’s how I have CUDA_LIBRARIES defined:


cuda.lib cudart.lib

Ah, that fixed it. Thanks!


Jared, this is very helpful. Thanks.

One problem I ran into was mixing .cpp and .cu files in the build.
The linker only seems to catch the .obj file from the nvcc step.
Adding the .cpp’s .obj file under the project’s property “Linker->Input->Add Module to Assembly” forced the linker to include the .obj file in the build.

Have you (or anyone else) run into this problem?

I don’t think I’ve run into this problem, somehow msvc just figures out the linker needs to include any .obj it finds in \Release.

Though, I have run into problems with including .cu & .cpp files with the same base name in my project. For example, foo.cpp & would only produce one .obj; whichever was compiled later. My workaround was to compile -> foo.cuda.obj. You can make this change pretty easily by modifying the build line.

I’ve only got one .cu file in the project.

Can you post the command line for the compile step? It sounds like something may be wrong with your environment variables.

right-click the .cu in msvc

click properties

click the + next to CUDA

click command line

This should be fixable by having .cpp objs and .cu objs go to separate output directories, or you could just make your rule compile *.cu files to *.cu.obj or something like that.


For EmuDebug mode, obviously.

I think the spaces in this path is your problem, try putting this include path in quotes when you define the environment variable.

Hi, I’m new to CUDA. I managed to compile my source code by nvcc, but I kept getting this error message when I was trying to link by Microsoft’s C++ Compiler:

main.obj : error LNK2019: unresolved external symbol “void __cdecl saxpy(float *,float const *,float,unsigned int)” (?saxpy@@YAXPAMPBMMI@Z) referenced in function _main

This both happened when I try to link via VS IDE and command prompt. Can somebody help me?

Thank you very much.

BTW, Jared’s custom build rule is indeed very helpful. Nice work, Jared!

Small bugfix (I think, because I’m not proficient in VS), a change in the CommandLine, from:

CommandLine="nvcc.exe -ccbin "$(VCInstallDir)bin" [Emulation] -c [Defines] -Xcompiler "/EHsc [Warning] /nologo /Wp64 [Optimization] /Zi [RuntimeChecks] [Runtime] [TypeInfo]" [Include] -o $(ConfigurationName)\$(InputName).obj $(InputFileName)"

to this:

CommandLine="nvcc.exe -ccbin "$(VCInstallDir)bin" [Emulation] -c [Defines] -Xcompiler "/EHsc [Warning] /nologo /Wp64 [Optimization] /Zi [RuntimeChecks] [Runtime] [TypeInfo]" [Include] -o $(ConfigurationName)\$(InputName).obj "$(InputPath)""

The $(InputFileName) does not work when the working directory is different than one containing the *.cu file.


I have a huge solution with 2 projects. and now I would like to use cuda to accelerate my program. I use the rule file to compile my .cu file, but there’s a error message: ( is my file name)

Catastrophic error: could not open source file “”
1 catastrophic error detected in the compilation of “”.
Compilation terminated.

How could I solve it?? thank you!!

My crystal ball is off today, sorry ;-)
Give us at least a command line with nvcc execution (from build log).

Anyway, have you tried my bugfix? I’ve got identical errors before applying it.

Hi, I’ve tried your bugfix and it works!! Thank you very much!

How i encounter another error.
I’ve added CUDA\include ,CUDA\lib, SDK\common\lib directories in the Options menu. and added the Additional Dependences in the Project Properties menu with cuda.lib, cudart.lib, cutil32.lib…
and here is my error message:

error LNK2019: unresolved external symbol _funct001 referenced in function _write_stuff

It seems like there’s a linking problem, but i’ve no idea how to solve it.
(all my source code is composed in C)
Does anyone know how to solve it?
Thank you!!

In order to open win32-console-app-cuda-msvc2005.vcproj

you first have to edit it in a text editor and change

to reflect the correct path…since you have named the archive with underscores, most people who simply extract it to “cuda_build_rule” will have an error when they try to open.


Thx for sharing the build rule. But I encounter an error when compiling:
‘nvcc.exe’ is not recognized as an internal or external command. Can anyone tell me how to solve it?