C compiler other than 'cl.exe' in windows

Is there any option in ‘nvcc’ to specify our own C compiler.
For eg.: by default, in windows, ‘cl.exe’ is considered to be the C compiler. Can I change this to, let us say, ‘gcc’ compiler for windows or a turbo C compiler?

I was just curious whether or not its possible! :)

Use --foreign

/cygdrive/c/CUDA/bin/nvcc --compiler-bindir "C:/cygwin/usr/bin" --foreign gcc -I "/cygdrive/c/Program Files/NVIDIA Corporation/NVIDIA CUDA SDK/common/inc" -I "/cygdrive/c/CUDA/include"  -D _CRT_SECURE_NO_DEPRECATE -D _CRT_NONSTDC_NO_DEPRECATE -c -o src/test.obj src/test.cu

This is resulting in an error message!

(running on cygwin)

I’ve gotten these error message before and they usually meant there were bogus command line options. Try removing the space between your -D and -I switches and the strings next to them.

e.g. -D_CRT_NONSTDC_NO_DEPRECATE and -I"/cygdrive/c/CUDA/include"

/cygdrive/c/CUDA/bin/nvcc --compiler-bindir "C:/cygwin/usr/bin" --foreign -I"/cygdrive/c/Program Files/NVIDIA Corporation/NVIDIA CUDA SDK/common/inc" -I"/cygdrive/c/CUDA/include"  -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -c -o src/test.obj src/test.cu

tried this… but now a window pops up saying that

“cudafe.exe encountered a problem and needs to close”


The gcc seems to have disappeared from your command line.

If I include ‘gcc’ then the previous error message is getting repeated again.
Also, ‘–foreign’ option is a ‘boolean switch’
i.e. default c compilers are ‘gcc/g++’ for linux and ‘cl’ for windows.
and presence of ‘–foreign’ will just reverse this default values (i.e. ‘gcc/g++’ for windows)

I found out this by: ‘nvcc --help’

gcc is only supported for Mac & Linux. MSVC is the only supported compiler for Windows.

Besides, which version of CUDA are you using that you’re running it under cygwin? I don’t think anything is supported under cygwin, you’d need a totally win32 gcc.

cuda version 2.0

can’t i use the ‘gcc’ provided by the cygwin environment as a C compiler for nvcc?

gcc is only supported for Mac & Linux. MSVC is the only supported compiler for Windows.

I bet you could hack it if you really tried… what does MSVC have that gcc don’t? GCC can interface to win32 dlls, in mingw, cygwin, and subsystem for unix applications (MS’s cygwin).

Surely if you used the Driver API none of this would even be a problem.

Could you please elaborate on what do you mean by this statement?

The driver api is the one that doesn’t use “nvcc.” It just uses a standard C compiler, you’ve got to include the header files yourself, and all the function calls get more complicated because the syntactic sugar goes away. But it should work 100% with gcc since gcc can work 100% with ordinary windows dlls. It’s nvcc’s “magic” that’s the problem. Look at the programming guide re driver api.

Thanks a lot!
But I couldn’t find a documentation for driver api. :(
Can you please give the link to it?

Look at the CUDA reference manual–the driver API commands begin with cu, instead of cuda.

thanks a lot! I got it finally! :)

A last (idiotic) question :)
So, if I use driver API (with appropriate header files) can I use ‘gcc’ (or any other C compiler) to compile my code for GPU?

Afaik in 2.3 where the kernel should be in .ptx or .cubin format, you cannot compile the kernel in gcc, as gcc knows nothing about these (Nvidia?) formats.

I got mine test working on win64 with mingw-w64 by using the driver api and 64bit linux nvcc to compile the kernel to .cubin (and .ptx). What is kind of annoying is that i cannot compile 64b and 32b versions on same nvcc, as it seems to support only native compiling. Also the message if you try to load 32b .cubin to 64bit cuda program you just get ‘cannot find function’-error.

Steps to compile program using driver-api:

  • create .def files for the nvcuda.dll that can be found from system32 directory (with for example mingw-w64 gendef tool)
  • create .a gcc library files (dlltool)
  • compile your program with gcc and link with libraries
  • compile your kernel on some OS that has same bitcount (linux compiled 64bit kernel worked on w64 .exe and 32b on w32 .exe)

I have not tested would it be possible (well in chroot at least it should be) to install both 32b and 64b nvcc to 64b os.

I would love to use the runtime api.

Some post say that one could use “–foreign” option to select compiler but in nvcc 2.3 it is not documented.

I also think that i saw (i cannot find it anymore!) a post about suggestion writing a wrapper for gcc that converts the input parameters from cl.exe to gcc but i have my doubts on that since the output of these two compilers are (??) not the same?

So, has anyone got runtime working in windows without using CL.exe?

I think that NVCC first runs the native compiler’s preprocessor to separate kernel code from CPU code; it must use the native compiler for that because (at least last time I looked) that’s the only way to assign the code to the proper sections inside object files. My own hobby projects use gcc and I primarily develop on windows, so I compromise by installing the free MSVC Express edition, let NVCC compile only device code, and use gcc for everything else. Note that if you use the driver API you can write C code to launch GPU kernels, that can compile with any compiler that can link with cuda.lib on windows. But the moment you start using keywords like global or device then you must have cl.exe or it won’t compile, even if it’s only device code that NVCC ends up compiling on its own and even if the output is just PTX.