juliamatlab / mexjulia

embedding Julia in the MATLAB process.
MIT License
52 stars 14 forks source link

Augment `LD_RUN_PATH` at runtime on *nix. #7

Closed twadleigh closed 8 years ago

twadleigh commented 9 years ago

In https://github.com/twadleigh/jlcall/issues/2#issuecomment-146190337 @bermanmaxim says:

Correction: apparently it is the setenv('LD_RUN_PATH', '/home/maxim/julia/lib/julia'); command that really helped, not LD_LIBRARY_PATH... And I have to set it at each new instantiation of Matlab for it to work. I added this LD_RUN_PATH definition to the initialization of the Jl class, and it conveniently works.

On windows, the analogous thing is achieved by adding julia_bin_dir to the system PATH. (Windows uses the same path for executables and shared libraries.) We need to provide the right analogous handling for POSIX systems.

bermanmaxim commented 9 years ago

Another solution is to link the shared library with -rpath during compilation -Wl,-rpath=$JULIA_DIR/lib/julia Tested to work on Ubuntu.

twadleigh commented 9 years ago

Yes, that's a better solution. Thanks.

omus commented 8 years ago

I've been having issues the dynamic libraries not being loaded on OS X. So far I haven't found a great solution as -rpath isn't working with clang but I found a work-around by explicitly setting the dynamic library paths. Note that ... should be replaced with an absolute path:

otool -L jlcall.mexmaci64
install_name_tool -change @rpath/libjulia.dylib .../lib/julia/libjulia.dylib jlcall.mexmaci64
install_name_tool -change @rpath/libmx.dylib .../lib/julia/libmx.dylib jlcall.mexmaci64
install_name_tool -change @rpath/libmex.dylib .../lib/julia/libmex.dylib jlcall.mexmaci64
install_name_tool -change @rpath/libmat.dylib .../libmat.dylib jlcall.mexmaci64
otool -L jlcall.mexmaci64

I'll keep looking for a good solution in OS X.

plzh commented 8 years ago

Hi @bermanmaxim,

You said:

Another solution is to link the shared library with -rpath during compilation -Wl,-rpath=$JULIA_DIR/lib/julia Tested to work on Ubuntu.

By adding -rpath to LDFLAGS, it builds on my PC. However, calling double_it.m encountered the following errors:

>> double_it
Error using jlcall
Unhandled Julia exception: ErrorException

Error in Jl.eval_string (line 29)
      Jl.mex(0, expr);

Error in Jl.include (line 33)
      Jl.eval_string(['include("' Jl.forward_slashify(fn) '")']);

Error in double_it (line 4)
Jl.include('double_it.jl');

I used Matlab 2015b under Ubuntu 15.10 64bit. The log for the mex command:

mex  -largeArrayDims -O -output jlcall -outdir '/home/plzh/Dropbox/work/jlcall/m' -I'/usr/include/julia' -L'/usr/lib/x86_64-linux-gnu/julia' '/home/plzh/Dropbox/work/jlcall/m/../src/jlcall.cpp' -ljulia -v LDFLAGS='\$LDFLAGS -Wl,-rpath=/usr/lib/x86_64-linux-gnu/julia'

-> mexopts.sh sourced from directory (DIR = $PREF_DIR)
   FILE = /home/plzh/.matlab/R2015b/mexopts.sh
----------------------------------------------------------------
->    MATLAB                = /usr/local/MATLAB/R2015b
->    CC                    = gcc-4.7
->    CC flags:
         CFLAGS             = -ansi -D_GNU_SOURCE  -fexceptions -fPIC -fno-omit-frame-pointer -pthread
         CDEBUGFLAGS        = -g
         COPTIMFLAGS        = -O -DNDEBUG
         CLIBS              = -Wl,-rpath-link,/usr/local/MATLAB/R2015b/bin/glnxa64 -L/usr/local/MATLAB/R2015b/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
         arguments          = 
->    CXX                   = g++-4.7
->    CXX flags:
         CXXFLAGS           = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
         CXXDEBUGFLAGS      = -g
         CXXOPTIMFLAGS      = -O -DNDEBUG
         CXXLIBS            = -Wl,-rpath-link,/usr/local/MATLAB/R2015b/bin/glnxa64 -L/usr/local/MATLAB/R2015b/bin/glnxa64 -lmx -lmex -lmat -lm
         arguments          = 
->    FC                    = gfortran
->    FC flags:
         FFLAGS             = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
         FDEBUGFLAGS        = -g
         FOPTIMFLAGS        = -O
         FLIBS              = -Wl,-rpath-link,/usr/local/MATLAB/R2015b/bin/glnxa64 -L/usr/local/MATLAB/R2015b/bin/glnxa64 -lmx -lmex -lmat -lm
         arguments          = 
->    LD                    = g++-4.7
->    Link flags:
         LDFLAGS            = -pthread -shared -Wl,--version-script,/usr/local/MATLAB/R2015b/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined -Wl,-rpath=/usr/lib/x86_64-linux-gnu/julia
         LDDEBUGFLAGS       = -g
         LDOPTIMFLAGS       = -O
         LDEXTENSION        = .mexa64
         arguments          =  -L/usr/lib/x86_64-linux-gnu/julia -ljulia
->    LDCXX                 = 
->    Link flags:
         LDCXXFLAGS         = 
         LDCXXDEBUGFLAGS    = 
         LDCXXOPTIMFLAGS    = 
         LDCXXEXTENSION     = 
         arguments          =  -L/usr/lib/x86_64-linux-gnu/julia -ljulia
----------------------------------------------------------------

-> g++-4.7 -c  -I/usr/include/julia -I/usr/local/MATLAB/R2015b/extern/include -I/usr/local/MATLAB/R2015b/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread  -O -DNDEBUG  "/home/plzh/Dropbox/work/jlcall/m/../src/jlcall.cpp" -o /home/plzh/Dropbox/work/jlcall/m/jlcall.o

In file included from /home/plzh/Dropbox/work/jlcall/m/../src/jlcall.cpp:2:0:
/usr/include/julia/julia.h:1521:5: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat]
/usr/include/julia/julia.h:1523:5: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat]
/usr/include/julia/julia.h:1525:5: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat]
-> g++-4.7 -O -pthread -shared -Wl,--version-script,/usr/local/MATLAB/R2015b/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined -Wl,-rpath=/usr/lib/x86_64-linux-gnu/julia -o  "/home/plzh/Dropbox/work/jlcall/m/jlcall.mexa64"  /home/plzh/Dropbox/work/jlcall/m/jlcall.o  -L/usr/lib/x86_64-linux-gnu/julia -ljulia -Wl,-rpath-link,/usr/local/MATLAB/R2015b/bin/glnxa64 -L/usr/local/MATLAB/R2015b/bin/glnxa64 -lmx -lmex -lmat -lm
twadleigh commented 8 years ago

LD_LIBRARY_PATH is ignored by MATLAB. Other attempts to circumvent this issue have been implemented. Please open a new issue if you continue to experience issues after updating.