Adding cuda code to an MFC project

I installed cuda toolkit 10.1. When I create a Console application (I use MSVC++ 2015 Professional) I define a simple global kernel and call it from inside the main. I manage to compile the project without an error.(Choosing Cuda from build dependency menu, changing target machine from 32 bit to 64 bit, adding cudart_static.lib as external library for building and changing the property of the file that contains cuda code from “C/C++” to “Cuda C/C++”).
But when I want to do the same thing with an MFC project I get the following errors:

1>------ Rebuild All started: Project: MFCApplication2, Configuration: Debug x64 ------
1>
1> C:\Users\mevlut\Documents\Visual Studio 2015\Projects\MFCApplication2\MFCApplication2>“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc.exe” -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64” -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include" -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -g -use_fast_math -D_WINDOWS -D_DEBUG -D_UNICODE -DUNICODE -Xcompiler “/EHsc /W1 /nologo /Od /FS /Zi /RTC1 /MTd " -o x64\Debug\cudaClass.h.obj “C:\Users\mevlut\Documents\Visual Studio 2015\Projects\MFCApplication2\MFCApplication2\cudaClass.h” -clean
1> cudaClass.h
1> Compiling CUDA source file cudaClass.h…
1>
1> C:\Users\mevlut\Documents\Visual Studio 2015\Projects\MFCApplication2\MFCApplication2>“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc.exe” -gencode=arch=compute_35,code="sm_35,compute_35" --use-local-env -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64” -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include" -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -use_fast_math -D_WINDOWS -D_DEBUG -D_UNICODE -DUNICODE -Xcompiler “/EHsc /W1 /nologo /Od /Fdx64\Debug\vc140.pdb /FS /Zi /RTC1 /MTd " -o x64\Debug\cudaClass.h.obj “C:\Users\mevlut\Documents\Visual Studio 2015\Projects\MFCApplication2\MFCApplication2\cudaClass.h”
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\tchar.h(643): warning : expression has no effect
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\include\atlcomcli.h(698): error : invalid nontype template argument of type “const _GUID *”
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\comdef.h(494): error : invalid nontype template argument of type “const _GUID *”
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\comdef.h(501): error : invalid nontype template argument of type “const _GUID *”
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\comdef.h(508): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(30): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(33): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(36): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(39): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(42): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(45): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(48): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(51): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(54): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(57): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(60): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(63): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(93): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(96): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(99): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(102): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(105): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(108): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(111): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(114): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(117): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(123): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(126): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(129): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(132): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(135): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(144): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(147): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(150): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(153): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(156): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(159): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(165): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(168): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(171): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(174): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(177): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(180): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(183): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(186): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(189): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(192): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(195): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(198): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(201): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(204): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(210): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(213): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(216): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(219): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(222): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(228): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(240): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(243): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(246): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(249): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(252): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(255): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(264): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(267): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(270): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(273): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(279): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(282): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(285): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(288): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(291): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(297): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(300): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(303): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(306): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(312): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(315): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(318): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(321): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(324): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(330): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(333): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(336): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(339): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(342): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(345): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(348): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(351): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(354): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(663): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(666): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(669): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(678): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(681): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(684): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(687): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(690): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(693): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(696): error : invalid nontype template argument of type “const _GUID *”
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\comdefsp.h(699): error : invalid nontype template argument of type “const _GUID *”
1> cudaClass.h
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\BuildCustomizations\CUDA 10.1.targets(762,9): error MSB3721: The command ““C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc.exe” -gencode=arch=compute_35,code="sm_35,compute_35" --use-local-env -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64” -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include” -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -use_fast_math -D_WINDOWS -D_DEBUG -D_UNICODE -DUNICODE -Xcompiler “/EHsc /W1 /nologo /Od /Fdx64\Debug\vc140.pdb /FS /Zi /RTC1 /MTd " -o x64\Debug\cudaClass.h.obj “C:\Users\mevlut\Documents\Visual Studio 2015\Projects\MFCApplication2\MFCApplication2\cudaClass.h”” exited with code 1.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Despite all my efforts I couldn’t resolve the problem. Can somebody help me in this please?

One possible approach: build your CUDA code into an ordinary windows DLL.

Call/link to the DLL from your MFC project.

Instructions for how to build a CUDA DLL in windows Visual Studio can be found in various places on this forum as well as around the web.

this will restrict my ability to modify the code, I want to modify the code in a faster way. Isn’t there a sample cuda implementation together with mfc somewhere?

There aren’t any ready-made sample projects provided by NVIDIA that I am aware of.

A quick google search on “cuda mfc” turned this up:

[url]https://teguhsatria.com/2010/07/04/cuda-mfc/[/url]

I wouldn’t be able to help you with that. It probably needs project updating to newer VS.

There are probably other examples, as well, to be found with google.

Since Visual Studio has the concept of solutions which can consist of multiple projects, creating a solution with a MFC project and also a CUDA DLL project seems fairly straightforward. It shouldn’t restrict your ability to edit the code in any way that I can think of. When you open the solution, you will have access to both projects, and you can click back and forth between files in each project.

Thank you for the link, although I was aware of that sample, it is an old version of cuda and VS sample, the recipe doesn’t apply to the current versions.
Following your suggestion I got MS VS 2019 version, and the same problem still exists. I deal with extreme optimization techniques. Using a dll is not not a good solution for me. Is there any other solution?

Do you still have this problem?
Can your code compile in MBCS mode? I ask because I am not sure CUDA can work with Unicode. I use CUDA with MFC in MBCS mode and it works fine.

Hello ryork,
I tried MBCS mode too, I couldn’t get an mfc project even with an empty cuda kernel statement compile without an error. Which versions of Cuda and Visual Studio are you using?

I have CUDA v10.1 and VS2017.

is there anyway you can share an empty mfc project with a very simple CUDA kernal used somewhere in it?

Yes, here’s one. I found that VS2017’s IDE is rather clunky for most of the project settings so I end up using a text editor to change them. I did that with mine. I copied and pasted most of the CUDA stuff into this project file. The solution is trivial and no different so I will not include it.
///////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>


$(VCTargetsPath)\BuildCustomizations



Debug
x64


Release
x64



{19A99427-1EEB-4148-8B7F-212A71474946}
FractalsGPU
FractalsGPU



Dynamic


Dynamic


Application
MultiByte
v141
10.0.17763.0


true


false










false
ObjectsDbg64</OutDir>
ObjectsDbg64</IntDir>
false
$(ProjectName)


ObjectsRel64</OutDir>
ObjectsRel64</IntDir>
false
true
$(ProjectName)


$(IncludePath)
AllRules.ruleset





Level4
WIN32;_MBCS;%(PreprocessorDefinitions)
./;$(CudaToolkitDir)/include;C:/Cuda/inc;D:/Dev/MFCext/Include;


Windows
cudart_static.lib;freeglut.lib;%(AdditionalDependencies)
$(CudaToolkitLibDir);C:/Cuda/Lib/X64/;D:/Dev/Bin;$(PlatformName);


compute_52,sm_52;compute_61,sm_61;compute_75,sm_75;
-Xcompiler “/wd 4819” %(AdditionalOptions)
./;C:/Cuda/inc
WIN32




Disabled
MultiThreadedDebugDLL


true
Default


MDd
64




MaxSpeed
MultiThreadedDLL
Use
stdafx.h
true


false
Default


MD
64




ProgramDatabase
Use
stdafx.h
true











Create























One more thing I just remembered - the install process doesn’t handle updating very well. You will have to fix all of your projects unless you do something I did which is making an environment variable to hold the version of CUDA installed which is 10.1 for me. That’s CUDA_VERSION in the project file.

FWIW, I posted a bug report about this but it was blown off. I think this is seriously broken so I will continue to do it myself.

I am facing the exact same error. Did you find any workaround?