Bug with Visual Studio Compiler in Matlab with working Fix

Hi :wave:

i tried to use acados with the Microsoft Visual C Compiler in Matlab.
I could successfully build it as described in the installation doc. But i did get the following error when running an example:

compiling C:\Diplomarbeit\_Matlab\acados\examples\acados_matlab_octave\..\..\interfaces\acados_matlab_octave\ocp_get_cost.c
Building with 'Microsoft Visual C++ 2019 (C)'.
MEX completed successfully.
compiling C:\Diplomarbeit\_Matlab\acados\examples\acados_matlab_octave\..\..\interfaces\acados_matlab_octave\ocp_get.c
Building with 'Microsoft Visual C++ 2019 (C)'.
MEX completed successfully.
compiling C:\Diplomarbeit\_Matlab\acados\examples\acados_matlab_octave\..\..\interfaces\acados_matlab_octave\ocp_eval_param_sens.c
Building with 'Microsoft Visual C++ 2019 (C)'.
MEX completed successfully.
acados MEX interface compiled successfully
Successfully rendered acados templates!
Error using acados_template_mex.compile_ocp_shared_lib
Building templated code as shared library failed.
Got status 2, result: [ 20%] Building C object CMakeFiles/model_pendulum.dir/pendulum_model/pendulum_expl_ode_fun.c.obj
[ 40%] Building C object CMakeFiles/model_pendulum.dir/pendulum_model/pendulum_expl_vde_forw.c.obj
[ 60%] Building C object CMakeFiles/model_pendulum.dir/pendulum_model/pendulum_expl_vde_adj.c.obj
[ 60%] Built target model_pendulum
[ 80%] Building C object CMakeFiles/ocp_pendulum.dir/acados_solver_pendulum.c.obj
[ 80%] Built target ocp_pendulum
[100%] Linking C shared library acados_ocp_solver_pendulum.dll
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file

%% Deleted many similar lines because of the character limit %%

C:/Diplomarbeit/_Matlab/acados/examples/acados_matlab_octave/../../lib/blasfeo.lib(blasfeo.dir/Release/kernel_dgemm_4x4_lib4.obj):(.xdata[$unwind$kernel_dgemm_nn_4x4_lib4]+0x3c):
undefined reference to `__GSHandlerCheck'
C:/Diplomarbeit/_Matlab/acados/examples/acados_matlab_octave/../../lib/blasfeo.lib(blasfeo.dir/Release/kernel_dgemm_4x4_lib4.obj):(.xdata[$unwind$kernel_dgemm_nn_4x4_vs_lib4]+0x10):
undefined reference to `__GSHandlerCheck'
C:/Diplomarbeit/_Matlab/acados/examples/acados_matlab_octave/../../lib/blasfeo.lib(blasfeo.dir/Release/kernel_dgemm_4x4_lib4.obj):(.xdata[$unwind$kernel_dgemm_nn_4x4_gen_lib4]+0x28):
more undefined references to `__GSHandlerCheck' follow
collect2.exe: error: ld returned 1 exit status
CMakeFiles\acados_ocp_solver_pendulum.dir\build.make:89: recipe for target 'acados_ocp_solver_pendulum.dll' failed
mingw32-make.exe[2]: *** [acados_ocp_solver_pendulum.dll] Error 1
CMakeFiles\Makefile2:138: recipe for target 'CMakeFiles/acados_ocp_solver_pendulum.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/acados_ocp_solver_pendulum.dir/all] Error 2
Makefile:134: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2


Error in ocp_generate_c_code (line 279)
    acados_template_mex.compile_ocp_shared_lib(obj.acados_ocp_nlp_json.code_export_directory)

Error in acados_ocp (line 203)
            ocp_generate_c_code(obj);

Error in minimal_example_ocp (line 110)
ocp = acados_ocp(ocp_model, ocp_opts, simulink_opts);

I could track down the cause to line 45 in compile_ocp_shared_lib.m

[ status, result ] = system('cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_ACADOS_OCP_SOLVER_LIB=ON -S . -B .');

Changing this line to

[ status, result ] = system(['cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DBUILD_ACADOS_OCP_SOLVER_LIB=ON -S . -B .']);

resulted in another error:

compiling C:\Diplomarbeit\_Matlab\acados\examples\acados_matlab_octave\..\..\interfaces\acados_matlab_octave\ocp_get_cost.c
Building with 'Microsoft Visual C++ 2019 (C)'.
MEX completed successfully.
compiling C:\Diplomarbeit\_Matlab\acados\examples\acados_matlab_octave\..\..\interfaces\acados_matlab_octave\ocp_get.c
Building with 'Microsoft Visual C++ 2019 (C)'.
MEX completed successfully.
compiling C:\Diplomarbeit\_Matlab\acados\examples\acados_matlab_octave\..\..\interfaces\acados_matlab_octave\ocp_eval_param_sens.c
Building with 'Microsoft Visual C++ 2019 (C)'.
MEX completed successfully.
acados MEX interface compiled successfully
Successfully rendered acados templates!
compiling acados_mex_create_pendulum.c
Error using mex
MEX cannot find library 'acados_ocp_solver_pendulum', specified with the -l option.
 MEX searched for a file with one of the following names:
 libacados_ocp_solver_pendulum.lib
acados_ocp_solver_pendulum.lib
 Verify the library name is correct. If the library is not
 on the existing path, specify the path with the -L option.


Error in make_mex_pendulum (line 133)
            mex(FLAGS, LDFLAGS, COMPDEFINES, COMPFLAGS, acados_include, template_lib_include, external_include, blasfeo_include, hpipm_include,...

Error in pendulum_mex_solver (line 49)
            make_mex_pendulum();

Error in acados_ocp (line 212)
            obj.t_ocp = mex_solver();

Error in minimal_example_ocp (line 110)
ocp = acados_ocp(ocp_model, ocp_opts, simulink_opts);

The cause of this error is that MSVC generates the file in the subdirectory Release. To fix this, one must either stop the execution after CMakeLists.txt is created (e.g. breakpoint at line 45 in compile_ocp_shared_lib.m) and insert this in line 36

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE C:/Diplomarbeit/_Matlab/acados/examples/acados_matlab_octave/getting_started/c_generated_code)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE C:/Diplomarbeit/_Matlab/acados/examples/acados_matlab_octave/getting_started/c_generated_code)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE C:/Diplomarbeit/_Matlab/acados/examples/acados_matlab_octave/getting_started/c_generated_code)

or move the files manually from Release one folder up…

Then the examples compile and run successfully.

I did find that in commit #930 the compiler detection was removed in the ancestor file compile_main.m.

Best regards,
Alex

PS: The first error message occurred exactly the same on 2 different PCs with different software versions (R2022b + MSVC2019, R2018b + MSVC2015).

Hi Alex,

thanks for reporting this!

I tried to address this in a PR: fix MEX with MSVC by FreyJo · Pull Request #1036 · acados/acados · GitHub

Could you check if that works for you.

The fix in Matlab is implemented as you suggested.
Regarding the fix in the generated CMakeLists.txt file, this should be done by changing the corresponding template, acados/interfaces/acados_template/acados_template/c_templates_tera/CMakeLists.in.txt at afe921cbd368543a7fe243d05e5378fcad9ea7f9 · acados/acados · GitHub

Can we somehow not hardcode the version in

"Visual Studio 16 2019"

Can you check the TODO that I left in the PR, maybe you have a solution.

Cheers,
Jonathan

Hi :wave:,

thank you for your fast response. For the sake of completeness, it should be mentioned that the conversation is continued at the PR. :slightly_smiling_face:

Best regards,
Alex

1 Like