giaf / hpipm

High-performance interior-point-method QP and QCQP solvers
Other
546 stars 130 forks source link

MATLAB interface on Windows not compiling #96

Closed lenvm closed 3 years ago

lenvm commented 3 years ago

I am trying to use HPIPM as a solver for a problem defined in MATLAB. README.md describes the steps for compiling the interface for Linux, however not for Windows. I compiled BLASFEO and HPIPM using CMake. I have set the path environment variables manually in MATLAB, however running the file compile_mex_all.m runs into multiple errors on line 158. Could you add some instructions how to compile the MATLAB interface on Windows?

giaf commented 3 years ago

Hi, thanks for the interest in HPIPM. Unfortunately I am on a leave from work and do not have access to a Windows PC for the near future. Which type of error do you get exactly? My best suggestion for the time being is to try to do something similar to what happens in here https://github.com/acados/acados/blob/master/interfaces/acados_matlab_octave/sim_compile_interface.m https://github.com/acados/acados/blob/master/interfaces/acados_matlab_octave/ocp_compile_interface.m which to what I know is supposed to work smoothly on Windows too. If you manage find out what the issue is, please also let me know.

lenvm commented 3 years ago

Thanks for your response!

After setting the environment variables, the error received when running compile_mex_all.m is:

Error using mex
MEX cannot find library 'hpipm' specified with the -l option.
MEX looks for a file with one of the names:
libhpipm.lib
hpipm.lib
Please specify the path to this library with the -L option.
Error in compile_mex_all (line 158)
mex(mex_flags, 'CFLAGS=\$CFLAGS -std=c99', hpipm_include, blasfeo_include, hpipm_lib, blasfeo_lib, '-lhpipm', '-lblasfeo', mex_files{ii});

During building no .lib files were created, so I suspect the problem is associated to building HPIPM and BLASFEO on Windows.

giaf commented 3 years ago

The .lib extension is the extension for a static library in Windows, which should be the default target in CMake. As a first step, you can check that blasfeo.lib and hpipm.lib are indeed created, and where they are located / installed (by default using CMake they will be somewhere in the newly created build folder, plus on the location where you explicitly install them). If they are not created (as you seem to suggest), what is the extension of the produced libraries? .a? .dll?

Then you should specify correctly the location of such libraries, such that mex can find them. By default these assumptions are made https://github.com/giaf/hpipm/blob/master/interfaces/matlab_octave/env.sh#L16 https://github.com/giaf/hpipm/blob/master/interfaces/matlab_octave/env.sh#L22

These "main folders" are used in here https://github.com/giaf/hpipm/blob/master/interfaces/matlab_octave/compile_mex_all.m#L54 so you may need to modify these lines if the structure of the folders in the BLASFEO and HPIPM installation path is different from what is assumed here.

In summary, you should make sure that the .lib libraries are created, and make sure that the overall path in hpipm_lib and blasfeo_lib in here https://github.com/giaf/hpipm/blob/master/interfaces/matlab_octave/compile_mex_all.m#L54 contains the correct path to the libraries (the same may apply to the include files, if you happen to also have issues with that). I hope this helps, unfortunately I'm not very familiar with the Windows OS.

lenvm commented 3 years ago

Thank you for the elaborate explanation. The problems lies with the generation of the .lib files. After running

mkdir build
cd build
cmake -G "MinGW Makefiles" ..
cmake -- build .

in each directory, only libblasfeo.a and libhpipm.a are generated. No .lib files are created. Do you have any instructions how to run CMake in order to have it create the .lib files?

lenvm commented 3 years ago

As CMakeLists.txt checks if the compiler is equal to Microsoft Visual C++ before creating the .lib files in https://github.com/giaf/hpipm/blob/master/CMakeLists.txt#L104, I have tried compiling with Microsoft Visual C++ 2019 (C). Compiling with Micorsoft Visual C (MSVC) works 👍

A number of steps had to be taken, described below:

1) Install BLASFEO

mkdir build
cd build
cmake ..
cmake --build .

copy file blasfeo.lib from build/Debug/ to lib/

2) Install HPIPM

mkdir build
cd build
cmake ..
cmake --build .

copy file hpipm.lib from build/Debug/ to lib/

3) Compile the mex files using Matlab

Please find my pull request https://github.com/giaf/hpipm/pull/99 that includes the changes.

giaf commented 3 years ago

Glad to see that it worked out, and thanks for the PR!