C++ 从命令行运行nvcc时出现问题

C++ 从命令行运行nvcc时出现问题,c++,visual-c++,cuda,nvcc,C++,Visual C++,Cuda,Nvcc,我需要从命令行使用nvcc编译一个cuda.cu文件。该文件为“vectorAdd_kernel.cu”,包含以下代码段: extern "C" __global__ void VecAdd_kernel(const float* A, const float* B, float* C, int N) { int i = blockDim.x * blockIdx.x + threadIdx.x; if (i < N) C[i] = A[i] + B[i];

我需要从命令行使用nvcc编译一个cuda.cu文件。该文件为“vectorAdd_kernel.cu”,包含以下代码段:

extern "C" __global__ void VecAdd_kernel(const float* A, const float* B, float* C, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N)
        C[i] = A[i] + B[i];
}
编译器创建文件vectorAdd_kernel.cpp4.ii和vectorAdd_kernel.cpp1.ii,然后停止以下输出:

C:\Users\Massimo\Desktop\Pluto>nvcc --cubin --use-local-env --cl-version 2010 vectorAdd_kernel.cu -keep -I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"

vectorAdd_kernel.cu

vectorAdd_kernel.cu

c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error: invalid redeclaration of type name "size_t"

C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\new(51): error: first parameter of allocation function must be of type## Heading ## "size_t"

C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\new(55): error: first parameter of allocation function must be of type "size_t"

你能帮我解决这个问题吗?

我也遇到过类似的问题

SourceAnnotations.h中生成中断的代码:

#ifdef  _WIN64
typedef unsigned __int64    size_t;
#else
typedef _W64 unsigned int   size_t;
#endif
我用这个
--编译器选项“-D\u WIN64”
添加了
\u WIN64
编译器符号。我的nvcc构建字符串如下所示:

nvcc kernel.cu --cubin --compiler-options "-D _WIN64"

我刚刚在Visual Studio 2017和Cuda v9.0中遇到了这个问题,它们试图使用
nvcc
从命令行进行编译。经过长时间的讨论,我意识到我的Visual Studio命令行工具被设置为使用
x86
控制器中的
cl.exe
,而不是
x64
。有多种方法可以解决此问题,其中一种方法是覆盖它查找编译器工具时使用的目录,例如:

nvcc -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\bin\HostX86\x64"  -o add_cuda add_cuda.cu
然后它工作得很好

我还将提到,我使用git工具中的
which.exe
实用程序来确定它正在访问哪个版本的
cl.exe
,但是windows自带的
where
命令也起作用

更新: 处理此问题的另一种方法(可能是更好的方法)是将Visual Studio环境变量正确设置为64位,如Enterprise edition:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
对于社区版,请在路径中将“社区”替换为“企业”

您还可以使用(例如)
--vcvars\u ver=14.0
选择工具集,该工具集选择14.0工具集,这是使用15.5版本的Visual Studio编译CUDA 9.1所必需的

然后,您可以简单地使用以下内容进行构建:

nvcc  -o add_cuda add_cuda.cu

VS社区2019:

打开VS 2019的x64本机工具命令提示符

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
   Creating library a.lib and object a.exp

c:\Users\AFP\Downloads\cuda_by_example\chapter03>
如果未为
x64
初始化环境,并且您已为VS 2019打开了x86本机工具命令提示符,请运行:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu

YOU GET LOT OF ERRORS ....

75 errors detected in the compilation of "C:/Users/AFP/AppData/Local/Temp/tmpxft_00004504_00000000-12_hello_world.cpp1.ii".

c:\Users\AFP\Downloads\cuda_by_example\chapter03>"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
   Creating library a.lib and object a.exp

c:\Users\AFP\Downloads\cuda_by_example\chapter03>

.cu文件中还有什么?为什么需要
-I
VC/include路径?为什么您需要
--使用本地环境
--cl版本
?您能否详细说明问题的原因,或者此解决方案如何解决问题?谢谢,greate tip。阅读本文后,我尝试了VS17 x64本机工具命令行提示符。工作正常。另一个解决方案是从“开始”菜单启动“x64原生工具VS 2017命令提示符”。对于我来说,使用CLION可以选择不同版本的visual studio。选择AMD64设置为与此答案相同
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu

YOU GET LOT OF ERRORS ....

75 errors detected in the compilation of "C:/Users/AFP/AppData/Local/Temp/tmpxft_00004504_00000000-12_hello_world.cpp1.ii".

c:\Users\AFP\Downloads\cuda_by_example\chapter03>"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
   Creating library a.lib and object a.exp

c:\Users\AFP\Downloads\cuda_by_example\chapter03>