fatal error: 'mex.h' file not found

Hi guys, I don’t think my nvcc is set up correctly. If I try and compile an example program I get the error:
$ nvcc -c AddVectors.cu
nvcc warning : The ‘compute_10’ and ‘sm_10’ architectures are deprecated, and may be removed in a future release.
AddVectors.cu:2:10: fatal error: ‘mex.h’ file not found
#include “mex.h”
1 error generated.

I typed the program in exactly as it was in my book, so I’m thinking there’s something wrong with the path the compiler is using to search for mex.h? I’m running OS X 10.9 and have Xcode 5.1 beta 5 installed.
Any help would be greatly appreciated, thanks.

There is nothing wrong with nvcc.

You need to pass the location of the mex.h.

On LInux the sequence to compile a mex file that uses cuff from inside Matlab would be:

!nvcc -c myfun.cu -Xcompiler -fPIC -I /usr/local/matlab/extern/include

mex myfun.o -L /usr/local/cuda/lib64 -lcudart -lcufft

On Mac, it should be something similar to:
!nvcc -c myfun.cu -Xcompiler -fPIC -I /Applications/MATLAB_R2013b.app/extern/include/

mex myfun.o -L /usr/local/cuda/lib -lcudart -lcufft

Thanks for that assistance, the program (appears) to compile correctly now after entering:

!nvcc -c AddVectors.cu -Xcompiler -fPIC -I /Applications/MATLAB_R2013a.app/extern/include/

mex AddVectorsCuda.cpp AddVectors.o -lcudart -lcufft -L /usr/local/cuda/lib

However when I try and actually run the program I get the message:

Invalid MEX-file ‘/blah…/AddVectorsCuda.mexmaci64’:
dlopen(/blah…/AddVectorsCuda.mexmaci64, 6): Library not
loaded: @rpath/libcudart.6.0.dylib
Referenced from: /blah…/AddVectorsCuda.mexmaci64
Reason: image not found

I’ve got

DYLD_LIBRARY_PATH=/usr/local/cuda/lib:/Developer/NVIDIA/CUDA-6.0/lib: and PATH=/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/local/cuda/bin:/Developer/NVIDIA/CUDA-6.0/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin

all set up in .bash_profile, and “libcudart.6.0.dylib” definitely exists in the expected location, but is MATLAB still not knowing where to look?

MATLAB is incredibly picky and tends to ignore environment variables sometimes and re-defines then on startup. You might need to redefine the variables that point to the correct libraries within MATLAB. This issue is not necessarily unique to MEX files either, it happens when starting executables that depends on external libraries as well. I’ve seen this happen on Linux platforms more often than Windows, but doesn’t surprise me that it happened on a Mac.

Something like this might work also:
Similar topic, different executable

Haha thanks, but your link just happens to be another one of my posts, and thankfully I overcame that problem with some great help from fellow users.

I came across this question by chance and paste our erratum below.

We realized that there is an error in our textbook example in Chapter 2.5 (Simple Vector Addition Using CUDA in page 28).

Errorneous code example (AddVectors.cu)

#include “AddVectors.h”
#include “mex.h”

global void addVectorsMask(float* A, float* B, float* C, int size)
int i = blockIdx.x;
if(i >= size)

C[i] = A[i] +B[i];


Since this is the .cu file, we don’t need the “mex.h” file.
Actually we already corrected in our example codes (downloaded from the publisher’s website), but we didn’t delete it in our manuscript by mistake.

After fix (AddVectors.cu):

#include “AddVectors.h”
// #include “mex.h”

global void addVectorsMask(float* A, float* B, float* C, int size)
int i = blockIdx.x;
if(i >= size)

C[i] = A[i] +B[i];


Block out or delete the line: #include “mex.h”

If you need further assistance, please visit our blog http://cookinggpu.blogspot.com/
Thank you and appologize for your convenience.

Jung W. Suh