Invalid MEX file
I'm trying to use a few MEX functions, but am running into an error in MATLAB saying XXX.mexw64 is not a valid Win32 application. This happens with most of the functions needing CGAL, everything else seems to be fine. Followed the instructions below:
Ok, did you already download CMake and VS2015?
- Open CMake-Gui.
- Set "Where is the sourcecode" to
./gptoolbox/mexand "Where to build the binaries" to./gptoolbox/mex/build.- Click on configure.
- A window opens. Select "Visual Studio 14 2015 Win64" for "Specify the generator for this project". Click "Finish" in the same window.
- Process the error messages CMake might throw and select the features you want for your build by checking/unchecking the associated checkboxes (e.g. WITH_ELTOPO).
- Click on configure again. If there are still error messages, then fix them. Repeat until there are no more error messages.
- Click on generate. There should be no error messages. If there are errors... fix them. :-)
- Click on open project, which opens the project in VS2015.
- In VS: Compile the project.
Originally posted by @maxfrei750 in https://github.com/alecjacobson/gptoolbox/issues/78#issuecomment-441975232
Configuration:
- MATLAB r2019b
- VS 2015 (Tried with VS 2019, same problem)
- Boost 1.71.0
- CMake 3.15.4
Any help would be much appreciated!
Do you use the 64- or the 32-bit version of Matlab? If you use the 32-bit version, then I would guess that you need to compile the mex file as 32-bit version as well, by selecting a different compiler in step 4.
I’m using a 64-bit version of Matlab.
Ok, then unfortunately, I don't think I can help you. I think I have seen this error before. However, I don't recall how or even if I solved it, since I moved away from Matlab for quite some time now. Perhaps @alecjacobson can help.
Did you build cgal in x64?
I've left cmake to download cgal and the precompiled binaries. I'm not too familiar with cmake, but I'm assuming it all gets compiled at the same time along with the rest of the solution? I've set Visual Studio to compile in x64.
I think you have to build cgal first: https://www.cgal.org/download/windows.html
Couldn't get it to work in the end. Switched to working on a Mac and it compiled without any issues.
Thanks for all the help!
I don't work on Windows but some students have successfully got this to compile. @rarora7777 maybe you have a tip?
I looked at my compile notes from a couple of months ago, and I had to do the following for CGAL.
- Download
libiglmyself and set theLIBIGL_ROOTenvironment variable. This is my default setup and I prefer thatgptoolboxdoesn't re-downloadlibiglanyway. - Comment out
gptoolbox_download_libigl()ingptoolbox'sCMakeLists.txtto force it to use the existing installation oflibigl. - I already have
mpiravailable on my system via thevcpkgpackage manager (https://github.com/microsoft/vcpkg).mpiris the Windows fork ofgmp, whichCGALdepends on. - When building
libigl, I use thevcpkgtoolchain file and comment outigl_download_cgal_deps()to usempirinstalled oflibgmpdownloaded by this function call. - I have not tested the functions using
eltopo, but functions callingCGALwork fine.
tl;dr The main issue is that gptoolbox forces gmp, but I've only ever been able to use CGAL with mpir on my machines.
If it works with mpir, do think we can automate using mpir instead of gmp on Windows within the cmake?
I can confirm that it also works with gmp. But I still had to make a couple of manual changes.
-
FindBLASincluded with CGAL was unable to find MKL on my system, even with the environment variablesMKLROOTandMKL_LIB_DIRset. I just removed it and forced the use ofFindBLASincluded with CMake. Note that CGAL no longer includes this module, but the version downloaded by libigl does. - MSVC by default creates executables in the configuration folder (
./mex/Releaseor./mex/Debug). MATLAB expects themexw64executables to be placed next to the.mand.cppsource, so the executables need to be copied to the parent folder./mex. - Finally, the
mexw64executables looks formpfr.dllandmpir.dllinstead oflibmpfr-4.dllandlibgmp-10.dll. Manually renaming the files does the trick, but there must be a way to make them refer to the correct filenames.
@rarora7777 I used MINGW and CMake 3.20.5 to compile the /mex on Windows 10. The 'Configure' and 'Generate' in Cmake were both successful finally, but when I used 'make' of Mingw to make them ,the errors in the following picture occured. Do you know why? Can you help me , please. Thank you very much.

@babydu12138 I'm not even sure if MinGW is supported. I believe @alecjacobson has only ever built it for MSVC on Windows. Is it possible for you to switch to MSVC?
I have never built gptoolbox on windows personally.
@babydu12138 I'm not even sure if MinGW is supported. I believe @alecjacobson has only ever built it for MSVC on Windows. Is it possible for you to switch to MSVC?
Okay, I'll try it.
@babydu12138 I'm not even sure if MinGW is supported. I believe @alecjacobson has only ever built it for MSVC on Windows. Is it possible for you to switch to MSVC?
Okay, I'll try it.
@rarora7777 I have Configured and generated the codes in mex with CMake sucessfully, but I don't know what should I do the next step, please tell me, thank you very much.
Great! Now you just need to copy all the executables to the base folder, as that's where MATLAB requires them to be. That is, copy or move all mexw64 files from gptoolbox/mex/build/Release to gptoolbox/mex.
In addition, you need the dynamic library dependencies. Copy libgmp-10.dll and libgmp-4.dll from gptoolbox/mex/external/libigl/external/gmp/lib to gptoolbox/mex.
Great! Now you just need to copy all the executables to the base folder, as that's where MATLAB requires them to be. That is, copy or move all
mexw64files fromgptoolbox/mex/build/Releasetogptoolbox/mex.In addition, you need the dynamic library dependencies. Copy
libgmp-10.dllandlibgmp-4.dllfromgptoolbox/mex/external/libigl/external/gmp/libtogptoolbox/mex.
Thank you very much! I have compiled them successfully!
Great! Now you just need to copy all the executables to the base folder, as that's where MATLAB requires them to be. That is, copy or move all
mexw64files fromgptoolbox/mex/build/Releasetogptoolbox/mex.In addition, you need the dynamic library dependencies. Copy
libgmp-10.dllandlibgmp-4.dllfromgptoolbox/mex/external/libigl/external/gmp/libtogptoolbox/mex.
I copyed the "libgmp-10.dll" and "libgmp-10.lib" from "gptoolbox/mex/external/libigl/external/gmp/lib" and "libmpfr-4.dll" and "libmpfr-4.lib" from "gptoolbox/mex/external/libigl/external/mpfr/lib". It works.