Error when building Matlab-generated C Code from C++ Project


first of all, thank you so much for creating this fantastic solver! I love its speed. :wink:

I generated C Code from my MPC problem in Matlab, and now want to build this C Code within a C++ project, i.e. I have a top-levlew CMakeList that includes and adds the subdirectory with the generated C Code.
When I include the “workspace.h” now in my top-level “main.cpp”, I get the following error: “cannot convert ‘double’ to ‘linsys_solver_type’ in initialization”.
I digged into the “workspace.h”, and found the issue in the creation of the object:

OSQPSettings settings = {
0.000000000000, // <-- THIS SEEMS TO BE THE ISSUE
#endif // PROFILING

I tried a few things, including the fix from the “Build MATLAB interface from sources and binaries”-post in this forum. That was casting this as a

(c_int) 0.000000000000,

However, this then gave me an

invalid conversion from ‘c_int {aka long long int}’ to ‘linsys_solver_type’ [-fpermissive]"

Going deeper, I found the

enum linsys_solver_type { QDLDL_SOLVER, MKL_PARDISO_SOLVER };

in “constants.h” that matches the OSQP documentation on Linear Systems Solvers. So I took that “QDLDL_SOLVER”-constant and replaced “(c_int) 0.000000000000,”.
This seemed to fix the type issue, but now I get an

undefined reference to ‘solve_linsys_qdldl’

which I cannot find out anymore where it comes from.

Building the generated C Code and running the “example.c” works like a charm, but I cannot get it run from a C++ project. Any idea what could fix the lin_sys_solver_type issue?

Best regards,

@andi, what compiler are you trying to build with?

Thank you for your quick reply. My C/C++ compiler is gcc/g++ version 6.5.0.

@andi, what compiler flags are you using, and can you try with a more recent version of GCC? I just tried compiling the exported C code with g++ and the example compiled perfectly fine for me with GCC version 9.1.1.

Also, what version of the matlab interface are you using? Is it the tip of the master branch, or one of the releases?

Hi @imcinerney,
Sorry for getting back so late!

I think I used to be on the matlab interface version 0.5. I reinstalled OSQP for the 0.6 version, and it works now!
The updated code generation, now with a ‘workspace.c’ file seems to do the trick for me!


@andi good to hear that it is working now.

We introduced the workspace.c file in the recent release because of some other issues that were noticed when using the generated code (e.g. multiple symbol definitions if the old header was included in multiple places). So now it should be safe to include it in more than one compilation unit without causing linker errors.