From 6886042c8e8b3be0728b2d9d228116dff63a6f02 Mon Sep 17 00:00:00 2001 From: Davis King Date: Thu, 4 Feb 2016 09:19:13 -0500 Subject: [PATCH] Now when you make a mex file with cmake it will only try to link to the BLAS and LAPACK that comes with MATLAB since trying to use any other BLAS or LAPACK generally makes MATLAB unstable. --- dlib/cmake_find_blas.txt | 23 +++++++++++++++++------ dlib/matlab/cmake_mex_wrapper | 12 +++++++++++- dlib/matrix/lapack/fortran_id.h | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/dlib/cmake_find_blas.txt b/dlib/cmake_find_blas.txt index d8cebf9ce..42b7638b0 100644 --- a/dlib/cmake_find_blas.txt +++ b/dlib/cmake_find_blas.txt @@ -23,6 +23,23 @@ SET(lapack_found 0) if (UNIX) message(STATUS "Searching for BLAS and LAPACK") + if (BUILDING_MATLAB_MEX_FILE) + find_library(MATLAB_BLAS_LIBRARY mwblas PATHS ${MATLAB_LIB_FOLDERS} ) + find_library(MATLAB_LAPACK_LIBRARY mwlapack PATHS ${MATLAB_LIB_FOLDERS} ) + if (MATLAB_BLAS_LIBRARY AND MATLAB_LAPACK_LIBRARY) + add_subdirectory(external/cblas) + set(blas_libraries ${MATLAB_BLAS_LIBRARY} cblas ) + set(lapack_libraries ${MATLAB_LAPACK_LIBRARY} ) + set(blas_found 1) + set(lapack_found 1) + message(STATUS "Found MATLAB's BLAS and LAPACK libraries") + endif() + # Don't try to link to anything other than MATLAB's own internal blas + # and lapack libraries because doing so generally upsets MATLAB. So + # we just end here no matter what. + return() + endif() + include(CheckTypeSize) check_type_size( "void*" SIZE_OF_VOID_PTR) @@ -61,14 +78,8 @@ if (UNIX) set(lapack_found 1) set(found_intel_mkl 1) message(STATUS "Found Intel MKL BLAS/LAPACK library") - if (BUILDING_MATLAB_MEX_FILE) - message(STATUS "\n!!!! Don't forget to set the BLAS_VERSION and LAPACK_VERSION environment variables to !!!!\n!!!! ${blas_libraries} so MATLAB doesn't explode when you run this mex file !!!!\n") - endif() endif() - if (BUILDING_MATLAB_MEX_FILE) - return() - endif() if (NOT found_intel_mkl) # Search for the needed libraries from the MKL. This time try looking for a different diff --git a/dlib/matlab/cmake_mex_wrapper b/dlib/matlab/cmake_mex_wrapper index d3ab8df7d..feb7af7d4 100644 --- a/dlib/matlab/cmake_mex_wrapper +++ b/dlib/matlab/cmake_mex_wrapper @@ -67,7 +67,17 @@ INCLUDE(InstallRequiredSystemLibraries) MACRO(add_mex_function name ) - ADD_LIBRARY(${name} MODULE ${name}.cpp ) + ADD_LIBRARY(${name} MODULE ${name}.cpp ) + if (UNIX) + # Doing this prevents our mex function from exporting any symbols + # other than mexFunction(). This sometimes doesn't matter but sometimes + # avoids causing errors or otherwise bad behavior in MATLAB. + if (DEFINED ENV{MATLAB_HOME}) + set_target_properties(${name} PROPERTIES LINK_FLAGS "-Wl,--version-script,$ENV{MATLAB_HOME}/extern/lib/glnxa64/mexFunction.map") + else() + set_target_properties(${name} PROPERTIES LINK_FLAGS "-Wl,--version-script,${MATLAB_HOME}/extern/lib/glnxa64/mexFunction.map") + endif() + endif() # Change the output file extension to a mex extension. if (WIN32) diff --git a/dlib/matrix/lapack/fortran_id.h b/dlib/matrix/lapack/fortran_id.h index 9efe321d9..4ad13d2a2 100644 --- a/dlib/matrix/lapack/fortran_id.h +++ b/dlib/matrix/lapack/fortran_id.h @@ -43,7 +43,7 @@ namespace dlib namespace lapack { // stuff from f2c used to define what exactly is an integer in fortran -#if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__) +#if (defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__)) && !defined(MATLAB_MEX_FILE) typedef int integer; typedef unsigned int uinteger; #else