gptoolbox icon indicating copy to clipboard operation
gptoolbox copied to clipboard

Invalid MEX file

Open mishaelnuh opened this issue 6 years ago • 19 comments

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?

  1. Open CMake-Gui.
  2. Set "Where is the sourcecode" to ./gptoolbox/mex and "Where to build the binaries" to ./gptoolbox/mex/build.
  3. Click on configure.
  4. A window opens. Select "Visual Studio 14 2015 Win64" for "Specify the generator for this project". Click "Finish" in the same window.
  5. 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).
  6. Click on configure again. If there are still error messages, then fix them. Repeat until there are no more error messages.
  7. Click on generate. There should be no error messages. If there are errors... fix them. :-)
  8. Click on open project, which opens the project in VS2015.
  9. 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!

mishaelnuh avatar Oct 10 '19 23:10 mishaelnuh

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.

maxfrei750 avatar Oct 11 '19 08:10 maxfrei750

I’m using a 64-bit version of Matlab.

mishaelnuh avatar Oct 11 '19 10:10 mishaelnuh

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.

maxfrei750 avatar Oct 11 '19 10:10 maxfrei750

Did you build cgal in x64?

oqilipo avatar Oct 11 '19 17:10 oqilipo

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.

mishaelnuh avatar Oct 11 '19 20:10 mishaelnuh

I think you have to build cgal first: https://www.cgal.org/download/windows.html

oqilipo avatar Oct 11 '19 22:10 oqilipo

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!

mishaelnuh avatar Oct 12 '19 20:10 mishaelnuh

I don't work on Windows but some students have successfully got this to compile. @rarora7777 maybe you have a tip?

alecjacobson avatar Jun 11 '20 14:06 alecjacobson

I looked at my compile notes from a couple of months ago, and I had to do the following for CGAL.

  • Download libigl myself and set the LIBIGL_ROOT environment variable. This is my default setup and I prefer that gptoolbox doesn't re-download libigl anyway.
  • Comment out gptoolbox_download_libigl() in gptoolbox's CMakeLists.txt to force it to use the existing installation of libigl.
  • I already have mpir available on my system via the vcpkg package manager (https://github.com/microsoft/vcpkg). mpir is the Windows fork of gmp, which CGAL depends on.
  • When building libigl, I use the vcpkg toolchain file and comment out igl_download_cgal_deps() to use mpir installed of libgmp downloaded by this function call.
  • I have not tested the functions using eltopo, but functions calling CGAL work 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.

rarora7777 avatar Jun 11 '20 15:06 rarora7777

If it works with mpir, do think we can automate using mpir instead of gmp on Windows within the cmake?

alecjacobson avatar Sep 04 '20 14:09 alecjacobson

I can confirm that it also works with gmp. But I still had to make a couple of manual changes.

  • FindBLAS included with CGAL was unable to find MKL on my system, even with the environment variables MKLROOT and MKL_LIB_DIR set. I just removed it and forced the use of FindBLAS included 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/Release or ./mex/Debug). MATLAB expects the mexw64 executables to be placed next to the .m and .cpp source, so the executables need to be copied to the parent folder ./mex.
  • Finally, the mexw64 executables looks for mpfr.dll and mpir.dll instead of libmpfr-4.dll and libgmp-10.dll. Manually renaming the files does the trick, but there must be a way to make them refer to the correct filenames.

rarora7777 avatar Oct 19 '20 15:10 rarora7777

@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.

error

jbHu67 avatar Jul 08 '21 12:07 jbHu67

@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?

rarora7777 avatar Jul 08 '21 17:07 rarora7777

I have never built gptoolbox on windows personally.

alecjacobson avatar Jul 08 '21 18:07 alecjacobson

@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.

jbHu67 avatar Jul 09 '21 01:07 jbHu67

@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.

jbHu67 avatar Jul 09 '21 11:07 jbHu67

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.

rarora7777 avatar Jul 09 '21 16:07 rarora7777

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.

Thank you very much! I have compiled them successfully!

jbHu67 avatar Jul 10 '21 08:07 jbHu67

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.

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.

HoBool avatar Oct 29 '21 19:10 HoBool