Tricks and Tips: Compiling CUDA mex files with Visual Studio

If you want to compile a CUDA *.mex file in  Visual Studio, do the following:
Configuration: Matlab 2010a, Visual Studio 2010, Intel 64bit machine.

1) File -> New Project;

2) Choose MFC DLL as well as the Name of the project and the Location; Choose OK;

3) Choose Next;

4) Choose Finish (do not change any selection);

5) Solution Explorer -> Right click on the project’s name -> Build Customizations -> CUDA 5.0 -> Ok;

6) Project -> Add New Item; Choose C++ File (.cpp); Choose a Name for the file;

7) Add the line #include “stdafx.h”; of course, include “mex.h” and <cuda.h>, <cuda_runtime.h> etc. as appropriate;

8) Write your code containing the mexFunction under the above include lines; this .cpp file should contain only CPU-side code, namely, the mex function and other C++ functions.
Note that the mex function as well as the C++ functions should not contain kernel function invocations (no <<< >>> syntax permitted); kernel invocations should be contained in “wrapper functions” declared in this .cpp file as extern “C” and then defined in a .cu file (see next point);

9) Project -> Add New Item; Choose C++ File (.cpp); Choose a Name for the file, but use the .cu extension; this .cu file should contain the needed __global__ as well as __device__ functions;

10) Add the following lines to the .def file


Replace TestMex with the name of your .cpp file;

11) Project -> Properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories -> Add C:Program FilesMATLABR2010aexterninclude;

12) Project -> Properties -> Configuration Properties -> Linker -> General -> OutPut File -> $(OutDir)$(ProjectName).mexw64;

13) Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> C:Program FilesMATLABR2010aexternlibwin64microsoftlibmex.lib;
C:Program FilesMATLABR2010aexternlibwin64microsoftlibmx.lib;

14) Project -> Properties -> Configuration Properties -> CUDA C/C++ -> Device -> Choose your compute capability;

15) Project -> Properties -> Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32;

16) Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> Add cudart.lib;

17) The directory containing the compiled mex library is ..x64Debug;

You can find a worked out example in the attached Visual Studio 2010 project.


Ps. Applying the proposed solution in a new VS project, you may receive the following error at compile time: error LNK2005: DllMain already defined in MSVCRT.lib
It is due to the interaction between MSC (Microsoft Foundation Classes)  and CRT (C Run-time Libraries).
To avoid the error you have to set what library (MSVCRT.lib) has to be excluded from the project, as follow:
1)    In Project -> Properties -> Configuration Properties -> Linker -> Input -> Ignore Specific Default Libraries set: “MSVCRT.lib”

2)    In C/C++ -> Code Generation -> Runtime Library , set: “Multi-threaded DLL (/MD)”

3)    In CUDA C/C++ -> Host -> Runtime Library , set: “Multi-threaded DLL (/MD)”

One thought on “Tricks and Tips: Compiling CUDA mex files with Visual Studio

  1. Anik says:

    I have successfully compiled my program for mex file generation from a cuda code. The solution given in the following link are working fine. But I cannot find where the output .mexw64 (generated compiled file) is getting stored.

    According to the website suggestion, I checked in the following path for the mex file: C:Program Files (x86)Microsoft SDKsCpp REST SDK for Visual Studio 2013SDKbinx64Debug
    But it was not there as well.

    The location in configuration properties–>General–>Output Directory: $(SolutionDir)$(Platform)$(Configuration)

    So, if you have any idea about this pls comment. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *