[英]How do I start a new CUDA project in Visual Studio 2008?
This is an incredibly basic question, but how do I start a new CUDA project in Visual Studio 2008? 这是一个非常基本的问题,但如何在Visual Studio 2008中启动新的CUDA项目? I have found tons and tons of documentation about CUDA related matters, but nothing about how to start a new project.
我已经找到了大量关于CUDA相关问题的文档,但没有关于如何开始新项目的文档。 I am working with Windows 7 x64 Visual Studio 2008 C++.
我正在使用Windows 7 x64 Visual Studio 2008 C ++。 I would really like to find some sort of really really basic Hello World app to just get a basic program compiling and running.
我真的想找到一些真正非常基本的Hello World应用程序来获得基本程序的编译和运行。
Edit: 编辑:
I tried your steps Tom. 我尝试了你的步骤汤姆。 I setup a console app.
我设置了一个控制台应用。 I then deleted the default .cpp it drops in and copied over the three files from the template project just to have something to compile.
然后我删除了默认的.cpp,它从模板项目中删除并复制了三个文件,只是为了编译。 When I compile that, template_gold.cpp complained about not having stdafx.h included, so i included that.
当我编译它时,template_gold.cpp抱怨没有包含stdafx.h,所以我把它包括在内。 Now the build fails with this:
现在构建失败了:
1>------ Build started: Project: CUDASandbox, Configuration: Debug x64 ------ 1>Compiling... 1>template_gold.cpp 1>Linking... 1>LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup 1>D:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe : fatal error LNK1120: 1 unresolved externals 1>Build log was saved at "file://d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm" 1>CUDASandbox - 2 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
NOTE With the release of version 3.2 of the CUDA Toolkit, NVIDIA now includes the rules file with the Toolkit as opposed to the SDK. 注意随着CUDA Toolkit 3.2版的发布,NVIDIA现在包含带有Toolkit的规则文件而不是SDK。 Therefore I've split this answer into two halves, use the correct instructions for your version of the Toolkit.
因此,我将这个答案分成两半,使用适用于您的Toolkit版本的正确说明。
NOTE These instructions are valid for Visual Studio 2005 and 2008. For Visual Studio 2010 see this answer . 注意这些说明适用于Visual Studio 2005和2008.对于Visual Studio 2010,请参阅此答案 。
CUDA TOOLKIT 3.2 and later CUDA TOOLKIT 3.2及更高版本
I recommend using the NvCudaRuntimeApi.rules
file (or NvCudaDriverApi.rules
if using the driver API) provided by NVIDIA, this is released with the toolkit and supports the latest compiler flags in a friendly manner. 我建议使用NVIDIA提供的
NvCudaRuntimeApi.rules
文件(如果使用驱动程序API,则使用NvCudaDriverApi.rules
),它随工具包一起发布,并以友好的方式支持最新的编译器标志。 Personally I would advise against using the VS wizard, but only because I really don't think you need it. 就个人而言,我建议不要使用VS向导,但这只是因为我真的不认为你需要它。
The rules file (installed into the Program Files\\Microsoft Visual Studio 9.0\\VC\\VCProjectDefaults
directory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application. 规则文件(安装在
Program Files\\Microsoft Visual Studio 9.0\\VC\\VCProjectDefaults
目录中)“教”Visual Studio如何编译和链接项目中的任何.cu文件到您的应用程序中。
NvCudaRuntimeApi.rules
(right click on the project, Custom Build Rules , tick the relevant box), see note 1 NvCudaRuntimeApi.rules
(右键单击项目, 自定义构建规则 ,勾选相关框),参见注释1 $(CUDA_PATH)\\lib\\$(PlatformName)
to the Additional Library Directories and in Linker -> Input add cudart.lib
to the Additional Dependencies ), see notes [2] and [3] $(CUDA_PATH)\\lib\\$(PlatformName)
到附加库目录和链接器 - >输入添加cudart.lib
到附加依赖项 ),见注释[2]和[3] $(CUDA_PATH)\\include
to the Additional Include Directories ), see note [3] $(CUDA_PATH)\\include
在附加包含目录中 ,参见注释[3] Some other tips: 其他一些提示:
<sdk_install_dir>\\C\\doc\\syntax_highlighting\\visual_studio_8
<sdk_install_dir>\\C\\doc\\syntax_highlighting\\visual_studio_8
I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008): 我还建议使用以下注册表项启用Intellisense支持(对于VS2005而不是VS2008,将9.0替换为8.0):
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"
Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. 顺便提一句,如果可能的话,我会提倡避免使用cutil ,而是自己检查。 Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (eg command line parsing).
NVIDIA不支持Cutil,它仅用于尝试将SDK中的示例集中在实际程序和算法设计上,并避免在每个示例中重复相同的事情(例如命令行解析)。 If you write your own then you will have much better control and will know what is happening.
如果你自己编写,那么你将拥有更好的控制权并且会知道发生了什么。 For example, the
cutilSafeCall
wrapper calls exit()
if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly! 例如,如果函数失败,
cutilSafeCall
包装器调用exit()
- 一个真正的应用程序(而不是样本)应该可以更优雅地处理失败!
CUDA TOOLKIT 3.1 and earlier CUDA TOOLKIT 3.1及更早版本
I would use the Cuda.rules
file provided by NVIDIA with the SDK, this is released alongside the toolkit and supports the latest compiler flags in a friendly manner. 我会使用NVIDIA提供的
Cuda.rules
文件和SDK,它与工具包一起发布,并以友好的方式支持最新的编译器标志。 Personally I would advise against using the VS wizard, but only because I really don't think you need it. 就个人而言,我建议不要使用VS向导,但这只是因为我真的不认为你需要它。
The rules file (in the C\\common directory of the SDK) "teaches" Visual Studio how to compile and link any .cu files in your project into your application. 规则文件(在SDK的C \\ common目录中)“教”Visual Studio如何编译和链接项目中的任何.cu文件到您的应用程序中。
Cuda.rules
(right click on the project, Custom Build Rules , browse for the rules file and ensure it is ticked) Cuda.rules
(右键单击项目, 自定义构建规则 ,浏览规则文件并确保勾选) $(CUDA_LIB_PATH)
to the Additional Library Directories and in Linker -> Input add cudart.lib
to the Additional Dependencies ), see note [2] below $(CUDA_LIB_PATH)
到Additional Library Directories和Linker - > Input add cudart.lib
到Additional Dependencies ),参见注释[ 2]下面 $(CUDA_INC_PATH)
to the Additional Include Directories ) $(CUDA_INC_PATH)
到其他包含目录 ) Some other tips: 其他一些提示:
<sdk_install_dir>\\C\\doc\\syntax_highlighting\\visual_studio_8
<sdk_install_dir>\\C\\doc\\syntax_highlighting\\visual_studio_8
I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008): 我还建议使用以下注册表项启用Intellisense支持(对于VS2005而不是VS2008,将9.0替换为8.0):
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"
Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. 顺便提一句,如果可能的话,我会提倡避免使用cutil ,而是自己检查。 Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (eg command line parsing).
NVIDIA不支持Cutil,它仅用于尝试将SDK中的示例集中在实际程序和算法设计上,并避免在每个示例中重复相同的事情(例如命令行解析)。 If you write your own then you will have much better control and will know what is happening.
如果你自己编写,那么你将拥有更好的控制权并且会知道发生了什么。 For example, the
cutilSafeCall
wrapper calls exit()
if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly! 例如,如果函数失败,
cutilSafeCall
包装器调用exit()
- 一个真正的应用程序(而不是样本)应该可以更优雅地处理失败!
NOTE 注意
NvCudaRuntimeApi.v3.2.rules
. NvCudaRuntimeApi.v3.2.rules
。 This means that instead of looking for the CUDA Toolkit in %CUDA_PATH% it will look in %CUDA_PATH_V3_2%, which in turn means that you can have multiple versions of the CUDA Toolkit installed on your system and different projects can target different versions. LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs
) or multiply defined symbols for standard library functions, then this should be your first suspect. LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs
)或多个定义的符号用于标准库函数,那么这应该是你的第一个嫌疑人。 What a great question!! 真是个好问题! For all the CUDA documentation out there, this is something that I've always thought was an obvious omission... In fact, I'm really glad I found this post, because after using CUDA for quite a while, I still hadn't found an official, correct way to get VS to produce a CUDA program from scratch.
对于那里的所有CUDA文档,我一直认为这是一个明显的遗漏...事实上,我很高兴我找到了这篇文章,因为在使用CUDA很长一段时间后,我仍然没有找到了一个正式的, 正确的方法让VS从头开始制作CUDA程序。
When I've needed to start a new CUDA program, I've always just copied and modified the " template " example from the SDK directory. 当我需要启动一个新的CUDA程序时,我总是只复制并修改SDK目录中的“ 模板 ”示例。 This may not be exactly what you're looking for, because it doesn't start fresh, but it is a quick way to get a CUDA-capable project working in VS with all the correct project/file names...
这可能不是你想要的,因为它没有重新开始,但它是一个快速的方法来获得一个支持CUDA的项目在VS中使用所有正确的项目/文件名...
Additional info about installing CUDA wizard on VS2008 can be found here and here 有关在VS2008上安装CUDA向导的其他信息,请访问 此处和此处
[edit] [编辑]
If you don't want to use wizard you have to setup CUDA lib/include/nvcc paths manually and add custom build rules to each new CUDA program. 如果您不想使用向导,则必须手动设置CUDA lib / include / nvcc路径,并为每个新的CUDA程序添加自定义构建规则。 For additional info how to do it take a look at Tom's Answer.
有关其他信息,请参阅Tom's Answer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.