Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matlab 2018b - Ubuntu 18.04 - Missing Gateway function #278

Closed
DWolfr opened this issue Jun 26, 2019 · 9 comments
Closed

Matlab 2018b - Ubuntu 18.04 - Missing Gateway function #278

DWolfr opened this issue Jun 26, 2019 · 9 comments

Comments

@DWolfr
Copy link

DWolfr commented Jun 26, 2019

Hi everyone,

i have been installing NLopt using the Ubuntu 18.04 normal CMake version (3.10.2), with

cmake -DMatlab_ROOT_DIR=/usr/local/MATLAB/R2018b/ -DCMAKE_INSTALL_PREFIX=$HOME/NLopt/install -DINSTALL_MEX_DIR=$HOME/NLopt/matlab/ -DNLOPT_PYTHON=OFF -DNLOPT_OCTAVE=OFF -DNLOPT_GUILE=OFF -DNLOPT_SWIG=OFF ..

(from a sub directory 'build') for the usage with Matlab 2018b. After

make
make install

i tried to run the matlab script

opt.algorithm = NLOPT_LD_MMA
opt.lower_bounds = [-inf, 0]
opt.min_objective = @myfunc
opt.fc = { (@(x) myconstraint(x,2,0)), (@(x) myconstraint(x,-1,1)) }
opt.fc_tol = [1e-8, 1e-8];
opt.xtol_rel = 1e-4
​
[xopt, fmin, retcode] = nlopt_optimize(opt, [1.234 5.678])
​
function [val, gradient] = myfunc(x)
    val = sqrt(x(2));
    if (nargout > 1)
        gradient = [0, 0.5 / val];
    end
end
function [val, gradient] = myconstraint(x,a,b)
    val = (a*x(1) + b)^3 - x(2);
    if (nargout > 1)
        gradient = [3*a*(a*x(1) + b)^2, -1];
    end
end

and received the error

Invalid MEX-file '/home/USERNAME/NlpSolver/NLopt/matlab/nlopt_optimize.mexa64': Gateway function is
missing.
​
Error in untitled (line 8)
[xopt, fmin, retcode] = nlopt_optimize(opt, [1.234 5.678])

I found the issue robotology/blockfactory#4, which describes an problem between CMake (namely the FindMatlab.cmake) and Matlab Version 2018b.
The newer versions of CMake got a fix, which solves this problem https://gitlab.kitware.com/cmake/cmake/issues/18391 .

I found an fix for the combination between CMake version 3.10.2 and Matlab version 2018b in robotology-dependencies/qpOASES#11, where the the new CMake FindMatlab.cmake (https://raw.githubusercontent.com/Kitware/CMake/v3.14.3/Modules/FindMatlab.cmake) file should be copied in the local 'cmake' folder of the repository and robotology-dependencies/qpOASES@899afd8 should be applied.

This solves the problem locally, such that the given Matlab example is working.

Is there an other (better) workaround for this problem?

@stevengj
Copy link
Owner

stevengj commented Jun 26, 2019

From reading the linked PR, it seems the problem was that mexFunction here was not getting exported.

Short of waiting for a new cmake release that incorporates the fix, we might be able to just add __declspec(dllexport) to our function definition?

Can you try patching src/octave/nlopt_optimize-mex.c to add the following line before void mexFunction(...) to see if it fixes the problem?

#ifdef _MSC_VER
__declspec(dllexport)
#endif

@DWolfr
Copy link
Author

DWolfr commented Jun 27, 2019

Thank you for your fast reply, I tested your fix, but it is not working. The same error remains

Invalid MEX-file '/home/disi/tmp/nlopt/matlab/nlopt_optimize.mexa64': Gateway function is missing.

Error in untitled (line 8)
[xopt, fmin, retcode] = nlopt_optimize(opt, [1.234 5.678])

@jschueller
Copy link
Collaborator

are you sure it got rebuilt and installed after patching ?

@DWolfr
Copy link
Author

DWolfr commented Jul 22, 2019

I erased the the whole repo and the $HOME/NLopt/matlab/ matlab folder and rebuild everything with the suggested patch from @stevengj. Unfortunately the error is not fixed.

@jschueller
Copy link
Collaborator

jschueller commented Jul 22, 2019

oh, right, this patch should not be needed on Linux, only for msvc.
could you give us the output of
nm nlopt_optimize.mexa64 | grep mexFunction

if this returns nothing you can try to add DLL_EXPORT_SYM before "void mexFunction", or directly __attribute__ ((visibility ("default")))

@EABN
Copy link

EABN commented Jul 29, 2019

Thanks, @dsieb! I updated CMake to version 3.10.2 and then I could use NLopt in Matlab 2018b - Ubuntu 18.04.

@nicolapiccinelli
Copy link
Contributor

I have the same problem on Ubuntu 18.04, any idea about?

  • CMake version 3.10.2
  • Matlab 2019b
  • GCC 7.4.0

the output of nm nlopt_optimize.mexa64 | grep mexFunction is 0000000000002590 t mexFunction

@nicolapiccinelli
Copy link
Contributor

I've solved the issue adding to the CMakeLists.txt in the octave folder the following line set_target_properties(nlopt_optimize-mex PROPERTIES COMPILE_FLAGS "-fvisibility=default")
after the matlab_add_mex and then recompile.

@jschueller
Copy link
Collaborator

#312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants