MantisBT - CMake
View Issue Details
0015786CMakeModulespublic2015-10-13 07:322016-03-07 09:12
Kerstin Keller 
 
normalminoralways
closedno change required 
Win64Windows7
CMake 3.3.2 
 
0015786: FindMatlab / matlab_add_mex does not work with 64-bit Matlab Version - Version detected as 32-bit Matlab
I have installed a 64bit Matlab Version (2015a) and CMake 3.3.2.
When writing a simple CMakeLists.txt script

cmake_minimum_required(VERSION 3.3)
project(easy_example)
find_package(MATLAB REQUIRED)

include_directories(Matlab_INCLUDE_DIRS)

matlab_add_mex(
  NAME easy_example
  SRC easy_example.cpp
)

It cannot find the mex library. The Output is

-- Could NOT find Matlab (missing: Matlab_MEX_LIBRARY) (found version "8.5")
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
Matlab_MEX_LIBRARY
    linked by target "easy_example" in directory D:/SourceCode/Matlab/easy_example
-- Configuring incomplete, errors occurred!
Try to run

find_package(MATLAB REQUIRED)

and then use

matlab_add_mex(
...
)

with a 64-bit Matlab Version.
Digging a little into the FindMatlab.cmake script, and outputted the directories where it searches for the directories (l.1342).

message(STATUS "The include dir ${MATLAB_INCLUDE_DIR_TO_LOOK}")
message(STATUS "The library dir ${_matlab_lib_dir_for_search}")

This gives me the following Output

-- The include dir C:/LegacyApp/Matlab15a/R2015a_64bit/extern/include
-- The library dir C:/LegacyApp/Matlab15a/R2015a_64bit/extern/lib/win32/microsoft

However, as my Matlab Version is 64bit, it library dir should be
C:/LegacyApp/Matlab15a/R2015a_64bit/extern/lib/win64/microsoft

This can be traced back to line 1276, where I added another message Output:
if(_matlab_64Build)
  set(_matlab_current_suffix ${_matlab_bin_suffix_64bits})
  message(STATUS "64bit Matlab")
else()
  set(_matlab_current_suffix ${_matlab_bin_suffix_32bits})
  message(STATUS "32bit Matlab")
endif()

CMake falsely detects a 32bit Matlab Version
-- 32bit Matlab
No tags attached.
Issue History
2015-10-13 07:32Kerstin KellerNew Issue
2015-10-13 07:49Raffi EnficiaudNote Added: 0039583
2015-10-13 07:59Kerstin KellerNote Added: 0039584
2015-10-13 08:04Raffi EnficiaudNote Added: 0039585
2015-10-13 08:12Kerstin KellerNote Added: 0039586
2015-10-13 08:14Raffi EnficiaudNote Added: 0039587
2015-10-13 08:59Brad KingStatusnew => resolved
2015-10-13 08:59Brad KingResolutionopen => no change required
2015-10-13 09:07Kerstin KellerNote Added: 0039589
2015-10-13 09:09Kerstin KellerNote Edited: 0039589bug_revision_view_page.php?bugnote_id=39589#r1930
2015-10-13 11:26Raffi EnficiaudNote Added: 0039592
2016-03-07 09:12Robert MaynardNote Added: 0040614
2016-03-07 09:12Robert MaynardStatusresolved => closed

Notes
(0039583)
Raffi Enficiaud   
2015-10-13 07:49   
Hi,

It will look for the 64 bits version only if you use a 64bit toolchain. What is your initial cmake command?
(0039584)
Kerstin Keller   
2015-10-13 07:59   
Ok, so this isn't really a bug, more a wrong invokation:
I invoked CMake only with

cmake . -B_build -G "Visual Studio 11 2012"

instead of

cmake . -B_build -G "Visual Studio 11 2012 Win64"

Of course a Win32 build will look for a Win32 Matlab Version. However instead of saying Could NOT find Matlab, the FindMatlab.cmake Script could be improved to give a WARNING such as "Compiling for 32bit, but only 64bit Matlab Version is installed"
(0039585)
Raffi Enficiaud   
2015-10-13 08:04   
What do you see when you add "MATLAB_FIND_DEBUG" to your invocation/command line?
(0039586)
Kerstin Keller   
2015-10-13 08:12   
My Output is:
-- [MATLAB] Search for Matlab from the registry unsuccessful, testing all supported versions
-- [MATLAB] Matlab root folders are 8.5;C:/LegacyApp/Matlab15a/R2015a_64bit
-- [MATLAB] Current version is 8.5 located C:/LegacyApp/Matlab15a/R2015a_64bit
-- [MATLAB] [DEBUG]_matlab_lib_prefix_for_search = lib | _matlab_lib_dir_for_search = C:/LegacyApp/M
atlab15a/R2015a_64bit/extern/lib/win32/Microsoft

So it makes sense, it was just unclear to me, why it specified the win32 as search Directory.
I think we can close the issue, as it is clearly not a bug with the Script, only wrong usage by the user.
(0039587)
Raffi Enficiaud   
2015-10-13 08:14   
If you think the documentation might be improved, please indicate how you think this can be clarified.

Thanks!
(0039589)
Kerstin Keller   
2015-10-13 09:07   
(edited on: 2015-10-13 09:09)
Ok, so if Matlab was found (Matlab_FOUND is TRUE) but the mex library was not (Matlab_MEX_LIBRARY is NOT_FOUND), this is a strong indicator that compiler Version (32/64) and Matlab Version (32/64) are not compatible.


In the case of 32 bit compiler / 64 bit matlab the file exists at

/extern/lib/win64/Microsoft/libmex.lib,

but the search path will be

/extern/lib/win32/Microsoft/

and thus CMake cannot find the library and Matlab_MEX_LIBRARY is set to NOT_FOUND. In this case there could be a WARNING, "Matlab_MEX_LIBRARY NOT_FOUND: Please check if compiler Version and Matlab Version are compatible"


By the way, can you Point me to any example of the usage of matlab_add_mex macro, besides the official CMake documentation? I was not able to find a single example of the usage. Even though I can now find Matlab correctly, I still get linker Errors concerning used Matlab functions, see

easy_example.obj : error LNK2019: unresolved external symbol mxGetPr referenced in function "void _
_cdecl mexFunction(unsigned __int64,struct mxArray_tag * * const,unsigned __int64,struct mxArray_ta
g const * * const)" (?mexFunction@@YAX_KQEAPEAUmxArray_tag@@0QEAPEBU1@@Z) [D:\SourceCode\Matlab\eCA
LImporter\_build\easy_example.vcxproj]
easy_example.obj : error LNK2019: unresolved external symbol mxCreateStructMatrix_730 referenced in
 function "void __cdecl createStructureArray(struct mxArray_tag * *)" (?createStructureArray@@YAXPE
APEAUmxArray_tag@@@Z) [D:\SourceCode\Matlab\eCALImporter\_build\easy_example.vcxproj]
D:\SourceCode\Matlab\eCALImporter\_build\Debug\easy_example.mexw64 : fatal error LNK1120: 2 unresol
ved externals [D:\SourceCode\Matlab\eCALImporter\_build\easy_example.vcxproj]

(0039592)
Raffi Enficiaud   
2015-10-13 11:26   
You may have a look to this:

https://github.com/MPI-IS/Grassmann-Averages-PCA/blob/master/CMakeLists.txt#L82-L98 [^]

https://github.com/MPI-IS/Grassmann-Averages-PCA/blob/master/CMakeLists.txt#L482-L488 [^]

For your suggestion about the warning, my opinion is that is could be erroneous or misleading for other use cases.

Please have a look to the documentation: it looks like you are missing the MX component.

https://cmake.org/cmake/help/v3.3/module/FindMatlab.html#command:matlab_add_mex [^]
(0040614)
Robert Maynard   
2016-03-07 09:12   
Closing resolved issues that have not been updated in more than 4 months.