18

I need to compile a cuda .cu file using nvcc from command line. The file is "vectorAdd_kernel.cu" and contains the following piece of code:

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];
}

I used the following command (I need to get a .cubin file):

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

The compiler creates files vectorAdd_kernel.cpp4.ii and vectorAdd_kernel.cpp1.ii then it stops with the following output:

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"

Can you please help me in solving this issue?

talonmies
  • 70,661
  • 34
  • 192
  • 269
user1738687
  • 457
  • 3
  • 12
  • 4
    What else is in the .cu file? Why do you need to `-I` the VC/include path? Why do you need `--use-local-env` and `--cl-version`? – harrism Oct 12 '12 at 00:12

3 Answers3

21

I just encountered this in Visual Studio 2017 and Cuda v9.0 trying to compile from the command line with nvcc. After a lengthy session I realised my Visual Studio Command line tools were setup to use cl.exe from the x86 director instead of the x64. There are a number of ways to resolve it, one way is to override the directory it looks for its compiler tools with - for example as in :

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

It then worked fine.

I'll also mention that I used the which.exe utility from the git tools to figure out what version of cl.exe it was accessing, but the where command - native to windows - works as well.

Update:

Another way - probably a better way - to handle this is to just set the Visual Studio environment variables correctly to 64 bits like this for the Enterprise edition:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64

For the Community edition substitute "Community" for "Enterprise" in the path.

You can also select the toolset with (for example) --vcvars_ver=14.0 which selects the 14.0 toolset, necessary to compile CUDA 9.1 with the 15.5 version of Visual Studio.

Then you can build simply with this:

nvcc  -o add_cuda add_cuda.cu
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 2
    Thanks, greate tip. After reading this I tried the VS17 x64 native tools command line prompt. Worked. – anhoppe Oct 05 '18 at 19:46
  • 2
    Another solution is to simply launch the "x64 Native Tools Command Prompt for VS 2017" from the start menu. – Dave P. Feb 22 '19 at 16:58
  • 1
    for me with CLION it is possible to choose different versions of visual studio. choose AMD64 set the same as this answer – oak May 03 '20 at 11:03
3

VS Community 2019:

Open a x64 Native Tools Command Prompt for VS 2019

**********************************************************************
** 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>

If environment is not initialized for x64 and you have opened x86 Native Tools Command Prompt for VS 2019, run:

**********************************************************************
** 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>
afp_2008
  • 1,940
  • 1
  • 19
  • 46
1

I've had similar problem.

The code where build breaks in SourceAnnotations.h :

#ifdef  _WIN64
typedef unsigned __int64    size_t;
#else
typedef _W64 unsigned int   size_t;
#endif

I've added _WIN64 compiler symbol with this --compiler-options "-D _WIN64". My nvcc build string looked like this:

nvcc kernel.cu --cubin --compiler-options "-D _WIN64"
Jon Surrell
  • 9,444
  • 8
  • 48
  • 54