How to generate mex file for .cu file?

How to generate mex file for .cu file? (I want to create a mex file for my CUDA code so that I can run it in MATLAB).

Thanks----- Anik

Here’s a quick tutorial from one of our forum members:

For newer MATLAB gpuArray syntax here’s an article direct from the vendor:

Simple example:

Google is your friend ;)

Thanks a lot for your reply. In the tutorial I saw that it is saying to choose from Visual Studio C++ 2010

File → New Project; then to Choose MFC DLL as well as the Name of the project and the Location; Choose OK; Choose Next; Choose Finish (do not change any selection); Solution Explorer → Right click on the project’s name → Build Customizations → CUDA 5.0 → Ok;

But the problem is in my visual studio c++ 2010, MFC DLL option is not showing and in Build customization CUDA 5.0 or any other version option is also not showing. I searched for the solution in the internet. But didnot get satisfactory answer. Can anyone help in this case please?


You could do it the other way around… choose a CUDA project and change the project type to DLL after the fact. If the CUDA project is not showing you’ll need to uninstall and reinstall the CUDA Toolkit for it to show up as an option…

thanks again for your reply. I tried that, i think it is working fine but again difficulty occured at the 10th step which is

  1. Add the following lines to the .def file
    LIBRARY “TestMex”

where to find this .def file and the TestMex?

can anyone please help me to create mex file from my cuda code? I m really in need of this.

Hello anik, here is a solution to your problem:

  1. Create a regular CUDA project, let’s call it ‘MyProject’.
  2. Right click on ‘MyProject’ and select ‘Properties’.
  3. Change ‘Configuration Properties > General > Configuration Type’ to ‘Dynamic Library (.dll)’
  4. Change ‘Configuration Properties > General > Target Extension’ to ‘.mexw32’ (or .mexw64 if you are running 64-bits matlab’)
  5. Under ‘Configuration Properties > CUDA C/C++ > Common > Additional Include Directories’ add ‘C:\Program Files\MATLAB\R2013a\extern\include’
  6. Under ‘Configuration Properties > Linker > Input > Additional Dependencies’ add ‘C:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft\libmex.lib’ (or where you lib is located).
  7. Finnaly, use the following trick to make sure the ‘mexFunction(…)’ is exported correctly
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

// The following lines must be located BEFORE '#include <mex.h>'
#ifdef _MSC_VER
#define DLL_EXPORT_SYM __declspec(dllexport)
#include <mex.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  // Computations goes in here
  mexPrintf("Hello matlab world\n");

Also, make sure that your build is correctly setup for 32-bit/64-bit matlab.
Type ‘computer’ in matlab to find out if you are running 64 or 32-bit.

>> MyProject
Hello matlab world

Good luck!

Thanks nutti for your reply. :) :)
But I have two queries.

  1. Do I have to create this regular CUDA project using visual studio C++ 2010 version?

  2. And the trick which u used to make sure the mexfunction() is exported correctly, will I have to write this trick in my CUDA code itself or in any other separate file?

Please reply. Thank you

Also if possible please tell me which CUDA version you are referring?


  1. Either 2010 or 2012 will most likely work, depending on which CUDA version you have… do your research ;)
  2. I believe this should be in the same *.cu / mex file that your code lives. A mex file is just a C file with the MATLAB header. You can incorporate the CUDA code into that same file… no need for more than 1 file.

Error 1 error C1083: Cannot open include file: ‘mex.h’: No such file or directory H:\My project\My project\ 10 1 My project

Error 2 error MSB3721: The command ““G:\bin\nvcc.exe” -gencode=arch=compute_20,code="sm_20,compute_20" --use-local-env --cl-version 2013 -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin” -I"C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft\libmex.lib” -IG:\include -IG:\include -G --keep-dir Debug -maxrregcount=0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_WINDLL -D_MBCS -Xcompiler “/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o Debug\ “H:\My project\My project\”” exited with code 2. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 6.5.targets 593 9 My project

Hi friends…Thanks for your kind replies.

@nutti—> I tried the way u have mentioned . It is working but above two errors are coming.

@vacaloca----> thanks… but I am finding above two errors. What to do now?? pls suggest. I am using CUDA 6.5 and visual studio 12.0

I am very close to my work. Pls help guys… Thanks

Is there any way to create mex file for CUDA code in ubuntu?
Other than using visual studio can we create mex file using ubuntu?

Please reply.Thanks!!

Is there no one to help me with the above mentioned error? Please guys help

Looks like you did something wrong setting up the include path for the mex.h file. Re-do the solution nutti gave you, I think you did step 5 wrongly. Check the other steps as well!

Thanks alot guys for your suggestions. I have sorted out the problems. But only following issues I could not solve. It says “cblas.h”, “matrix_funcs.h” and “mex.h” source files cannot be opened. I have searched in the Program Files\MATLAB\R2014a\extern\include directory. mex.h file is available but I cant get it why it cannot be opned. Other two files are not available in the directory. Even I have not used other two header files in my code as well but still dont know why it is asking for “cblas.h” and “matrix_funcs.h”

any good suggestion will really be appreciated. Thanks…


External Media