Why does Matlab link to wrong library?

4 views (last 30 days)
Andrew Cox
Andrew Cox on 25 Apr 2017
Answered: Ankita Nargundkar on 12 May 2017
I'm compiling a C++ file that uses the GSL libraries on a Mac (Apple OS 10.12.4). Here's the compilation command:
mex GslInteg_CR3BP.cpp ../SPICE_MAC/cspice.a -I../include -I../SPICE_MAC/src/cspice -L../libmac -lgsl -lgslcblas
The directory structure is setup like:
  • CPP_fcns
  • include
  • libmac
  • SPICE_MAC
The GslInteg_CR3BP.cpp file is located within the CPP_fcns directory and the mex command is run from within that directory. Within the libmac folder are several static and dynamic libraries for GSL: libgsl and libgslcblas. When I run the compilation command, everything completes successfully. However, when I try to use the compiled executable I receive an error:
Invalid MEX-file
...
Library not loaded: /usr/local/lib/libgsl.0.dylib
...
Reason: image not found
I can confirm that the executable is linked incorrectly by checking with otool:
otool -L GslInteg_CR3BP.mexmaci64
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
/usr/local/lib/libgsl.0.dylib (compatibility version 18.0.0, current version 18.0.0)
/usr/local/lib/libgslcblas.0.dylib (compatibility version 1.0.0, current version 1.0.0)
@rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.50.2)
I don't understand why it's linking to files in /usr/local/lib. The GSL libraries do not exist there (I checked), nor are the libraries in ../libmac symbolically linked to any libraries in /usr/local/lib. I used to have GSL installed there, but I removed it. However, how is the compiler executing without linking errors if the libraries the *.mexmaci64 file are linked to do not exist? How do I force the compiler to link to the libraries in ../libmac?

Answers (1)

Ankita Nargundkar
Ankita Nargundkar on 12 May 2017
Contact MathWorks technical support for further assistance.

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!