Search code examples
matlabcmakemex

Cmake/matlab: FIND_PROGRAM does not find mex and mexext


I'm absolutely new to compiling stuff and am trying to cmake a CMakeList.txt with following (reduced) content (comments are my interpretation of what the code does):

FIND_PROGRAM(MEX_CMD mex)  # find programm mex and save in variable MEX_CMD
FIND_PROGRAM(MEXEXT_CMD mexext) # same for mexext

IF(MEX_CMD AND MEXEXT_CMD) # returns true if both variables exist, currently always returns false
    ...
ELSE()
MESSAGE(SEND_ERROR
    "Cannot find MATLAB or Octave instalation. Make sure that the 'bin' directory from the MATLAB instalation is in PATH"
)

Since the if statement always returns FALSE and I get the error message, I suppose it doesn't find mex or/and mexext. I tried to add the directory which contains both files to the path without any effect:

INCLUDE_DIRECTORIES(/usr/local/MATLAB/R2016a/bin)

Now i'm out of ideas. Where could the problem be? On a side note, in bash which -a mexext returns nothing. I'm using ubuntu 16.10, cmake 3.5.2.


Solution

  • According to find_program documentation you have several possibilities to "hint" CMake about location of the program.

    Via modification of CMakeLists.txt (if you are the author of the script):

    • PATHS or HINTS option for find_program:

      find_program(MEX_CMD mex PATHS /usr/local/MATLAB/R2016a/bin)
      
    • set CMake variable CMAKE_PROGRAM_PATH:

      list(APPEND CMAKE_PROGRAM_PATH "/usr/local/MATLAB/R2016a/bin")
      
    • set CMake variable CMAKE_PREFIX_PATH (without /bin suffix):

      list(APPEND CMAKE_PREFIX_PATH "/usr/local/MATLAB/R2016a")
      

    Without modification of CMakeLists.txt:

    • Set PATH environment variable (from the shell, before executing cmake):

       export PATH=$PATH:/usr/local/MATLAB/R2016a/bin
      
    • Pass CMake variable CMAKE_PROGRAM_PATH to the cmake:

       cmake -DCMAKE_PROGRAM_PATH=/usr/local/MATLAB/R2016a/bin <...>
      
    • Pass CMake variable CMAKE_PREFIX_PATH (without /bin suffix) to the cmake:

       cmake -DCMAKE_PREFIX_PATH=/usr/local/MATLAB/R2016a <...>