0

I'm having hard time trying to compile the following sample code:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <engine.h>

int main(int argc, char **argv )
{
    Engine *m_pEngine;

    if (!(m_pEngine = engOpen("\0"))) {
        fprintf(stderr, "\nCan't start MATLAB engine\n");
        return EXIT_FAILURE;
    }

    std::cout << "I'm alive!" << std::endl;

    return 0;
}

which is suppose to call a Matlab function using the engine interface. Compiling it with the following compiler's options:

g++ -Wall -pedantic main.cpp -o main -I/usr/local/MATLAB/R2013a/extern/include -L/usr/local/MATLAB/R2013a/bin/glnxa64 -leng -lmat -lut -lmx -std=c++11

gives thousand error messages => Link

After reading and searching in internet and in this forum I found the following answers #1, #2, #3 helpful and I can compile the same program simply setting the LD_LIBRARY_PATH of my system like the following:

declare -x LD_LIBRARY_PATH="/home/lukas/workspace_ros/devel/lib:/home/lukas/workspace_ros/devel/lib/x86_64-linux-gnu:/opt/ros/indigo/lib:/opt/ros/indigo/lib/x86_64-linux-gnu:/usr/local/MATLAB/R2013a/bin/glnxa64"

BUT another program, which is sharing the same variable (ROS), is not working anymore and I get the following error message:

lukas@ubuntu:~$ roscore
Traceback (most recent call last):
  File "/opt/ros/indigo/bin/roscore", line 62, in <module>
    import roslaunch
  File "/opt/ros/indigo/lib/python2.7/dist-packages/roslaunch/__init__.py", line 48, in <module>
    import rospkg
  File "/usr/lib/python2.7/dist-packages/rospkg/__init__.py", line 43, in <module>
    from .rospack import RosPack, RosStack, \
  File "/usr/lib/python2.7/dist-packages/rospkg/rospack.py", line 35, in <module>
    from xml.etree.cElementTree import ElementTree
  File "/usr/lib/python2.7/xml/etree/cElementTree.py", line 3, in <module>
    from _elementtree import *
ImportError: PyCapsule_Import could not import module "pyexpat"

So I must set the environment variable back and log out-in again to work with ROS.

So it seems to me that Matlab and ROS are not actually seeing and identifying their paths in that environment variable. Using one excludes the resource for the other one. How can they coexist in the same system? How should I set this variable?

UPDATE #1: according to Michael's suggestion I tried the following shell command:

LD_LIBRARY_PATH="/usr/local/MATLAB/R2013a/bin/glnxa64" g++ -Wall -pedantic main.cpp -o main -I/usr/local/MATLAB/R2013a/extern/include -L/usr/local/MATLAB/R2013a/bin/glnxa64 -leng -lmat -lut -lmex -std=c++11

and it compiles!!! :) So an upvote to Michael.

But the problem persists: if I try to run the program I get the following error message:

:~$ ./main
./main: error while loading shared libraries: libeng.so: cannot open shared object file: No such file or directory

that's because the library are shared and should be found at running time. Any idea???

Regards

Community
  • 1
  • 1
Dave
  • 349
  • 4
  • 22
  • 1
    What if you set `LD_LIBRARY_PATH` for your `g++` call only instead of exporting it? Like `LD_LIBRARY_PATH="path1:path2:path3" g++ -Wall -pedantic [...]` – Michael Jaros Mar 04 '15 at 20:08
  • 1
    Write a wrapper .sh script, which sets LD_LIBRARY_PATH before running a program, which requires custom libs. Alternatively, research *rpath*. – hyde Mar 04 '15 at 22:12
  • Thanks for your suggestion I was right now investigating that possibility. But the problem is: I need the matlab function (called over engine) to be called from a C++ application which is going to be compiled with a CMakeLists.txt file. So the wrapper should be integrated in that file! – Dave Mar 04 '15 at 22:21
  • can you please also add an output from `ldd matlablib.so`. Generally if matlab libs were linked against particular version of libstdc++, you should also use the same version. As various versions might have different ABI. This might also apply to other libs. – ibre5041 Mar 04 '15 at 22:41
  • 1
    you can also use `-Wl,-rpath=...` to compile-in library search path into your binary. – ibre5041 Mar 04 '15 at 22:43
  • I cannot find it (I mean matlablib.so). From the command line it says: `ldd: ./matlablib.so: file or directory not found` A search in MATLAB/ didn't find anything with such a name – Dave Mar 04 '15 at 22:50
  • Those libs libeng libmat libut libmex are they all part of matlab instalation? I ment those. – ibre5041 Mar 05 '15 at 08:23
  • yes! They are in the right folder and I can "see" them – Dave Mar 05 '15 at 10:32
  • Here is what I did to make it work on Linux Mint 17.2 with MATLAB R2013a: https://www.youtube.com/watch?v=iuOTf7mTooE Also, the answer here, about strace helped me out. http://stackoverflow.com/questions/1993865/connect-to-matlab-engine-from-c-in-linux –  Nov 23 '15 at 19:45

0 Answers0