Search code examples
matlabmex

Setting the output directory of mex files in the compiler command


My Project has the following structure:

MainFolder:
  >>InitToolbox.m     //Here addpaths are executed
  >>Compile.m         //Here mex compilations calls are made
    AlgorithmsFolder  //MATLAB code
    UtilitiesFolder   //MATLAB code
    MexFolder         // C++ files
       >>test1.cpp
       >>test2.cu

Whever I run (either in compile.m or directly in the command line) the following compiler call:

mex -v -largeArrayDims ./MexFolder/test1.cpp ./MexFolder/test2.cu

The output test1.mexw64 is saved in MainFolder.

Is there any way to modify the compiler call to create the .mexw64 file either on the original location of the files or in an specific user defined location?


Solution

  • You want to specify the output directory using the outdir option for mex

    mex -v -largeArrayDims ./MexFolder/test1.cpp ./MexFolder/test2.cu -outdir output_directory
    

    "output_directory" above can be any path you want.

    It can also be a variable, but that would require you to update the way you're calling mex

    outputFolder = 'path/to/folder';
    
    mex('-v', '-largeArrayDims', 'MexFolder/test1.cpp', ...
        'MexFolder/test2.cu', '-outdir', outputFolder);