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

Building gmtmex on Linux #8

Open
ahankak opened this issue Aug 12, 2019 · 2 comments
Open

Building gmtmex on Linux #8

ahankak opened this issue Aug 12, 2019 · 2 comments

Comments

@ahankak
Copy link

ahankak commented Aug 12, 2019

While searching for documentation regarding building gmtmex on Linux, I noticed that the readme was looking for volunteers. With the steps outlined below, I am able to get GMT to work with my MATLAB project on Ubuntu 18.04. I am sure there is a more elegant way of achieving this, however, I thought it best to share this solution, since it does appear to work well.

With both MATLAB R2019a and GMT Version 6.1.0_3ede8d1_2019.08.04 installed in /home, in the gmtmex directory cloned from GitHub:

  1. Built gmtmex object file as
    gcc -I$GMT_dir/include/gmt -I$MATLAB_dir/extern/include -O2 -Wall -m64 -fPIC -fno-strict-aliasing -std=c99 -DGMT_MATLAB -I$GMT_dir/include/gmt -c gmtmex.c

  2. Built gmtmex_parser object file as
    gcc -I$GMT_dir/include/gmt -I$MATLAB_dir/extern/include -O2 -Wall -m64 -fPIC -fno-strict-aliasing -std=c99 -DGMT_MATLAB -I$GMT_dir/include/gmt -c gmtmex_parser.c

  3. Compiled mex file as
    $MATLAB_dir/bin/mex -DGMT_MATLAB -I$GMT_dir/include/gmt -I/$GMT_dir/include/gmt/lib -I$MATLAB_dir/extern/include gmtmex.o gmtmex_parser.o -L$GMT_dir/lib -lgmt -L$MATLAB_dir/bin/glnxa64 -lmx -lmex -output gmtmex.mexa64

Copied over the gmt.m and generated gmtmex.mexa64 files to $GMT_dir/bin, added $GMT_dir along with subfolders to MATLAB path.

In order to prevent runtime errors associated with MATLAB not being able to find shared libraries, launch MATLAB as:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GMT_dir/lib
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtiff.so.5:/usr/lib/x86_64-linux-gnu/libstdc++.so.6
$MATLAB_dir/bin/matlab

I hope this proves to be helpful. Please let me know in case further tests are required.

@welcome
Copy link

welcome bot commented Aug 12, 2019

👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. We appreciate that you took the time to contribute!

@thomasAmorrow
Copy link

I have had success building gmtmex following ahankak's instructions above, using MATLAB R2019a and GMT 6.1.0_58a5b88_2019.09. In my case, MATLAB and GMT are in the standard installation locations. The following commands worked for me, hopefully they may help others:

  1. Build gmtmex in gmtmex src directory
    gcc -I/usr/local/include/gmt -I/usr/local/MATLAB/R2019a/extern/include -O2 -Wall -m64 -fPIC -fno-strict-aliasing -std=c99 -DGMT_MATLAB -I/usr/local/include/gmt -c gmtmex.c

  2. Build parser
    gcc -I/usr/local/include/gmt -I/usr/local/MATLAB/R2019a/extern/include -O2 -Wall -m64 -fPIC -fno-strict-aliasing -std=c99 -DGMT_MATLAB -I/usr/local/include/gmt -c gmtmex_parser.c

  3. Compile
    /usr/local/MATLAB/R2019a/bin/mex -DGMT_MATLAB -I/usr/local/include/gmt -I/usr/local/include/gmt/lib -I/usr/local/MATLAB/R2019a/extern/include gmtmex.o gmtmex_parser.o -L$GMT_dir/lib -lgmt -L/usr/local/MATLAB/R2019a/bin/glnxa64 -lmx -lmex -output gmtmex.mexa64

To run MATLAB with gmtmex capabilities, I have to perform the same library modifications
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GMT_dir/lib
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtiff.so.5:/usr/lib/x86_64-linux-gnu/libstdc++.so.6
matlab &

I tried for some time to use MATLAB's setenv functions to get these libraries working, but with no luck. I also tried putting the export commands in my .profile, but that also did not seem to work. The only way I can get MATLAB to start in an automated sense is to make a shell script:

#!/bin/bash

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GMT_dir/lib
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtiff.so.5:/usr/lib/x86_64-linux-gnu/libstdc++.so.6

/usr/local/MATLAB/R2019a/bin/matlab -desktop &

Hopefully someone with a little more MATLAB experience can figure out a way to include these library modifications in the startup.m file, I tried a bit but had no luck.

In the past, I have been using GMT through MATLAB by invoking the following
setenv LD_LIBRARY_PATH usr/lib/x86_64-linux-gnu/
before calling GMT functions using the system function. Here's an example of how I used to do this, passing MATLAB variables in to GMT
system(['grdmath ' num2str(rho_c) ' ' num2str(rho_w) ' SUB ' num2str(rho_m) ' ' ...
num2str(rho_c) ' SUB DIV ED.grd MUL NEG = ' grdfile '_airy_compensation.grd']);

The new gmtmex functionality seems like a great improvement over this type of tedious coding, I hope we can smooth out the Linux compatibility with a little effort from the community.

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

2 participants